blob: d7dc21157af9eced062b850c9e9b49da2dd45899 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan67340072011-04-16 19:23:10 +000027# drop_all_tables ?DB?
dand05a7142016-08-02 17:07:51 +000028# drop_all_indexes ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000029# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000030# 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#
mistachkinc60941f2012-09-13 01:51:02 +000035# capable EXPR
danc1a60c52010-06-07 14:28:16 +000036# 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#
mistachkin1d406e02013-08-29 01:09:14 +000046# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000047# 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?
drh433dccf2013-02-09 15:37:11 +000059# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000060# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000061# do_execsql_test TESTNAME SQL EXPECTED
62# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000063# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000064#
dan430e74c2010-06-07 17:47:26 +000065# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000066#
67# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000068# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000069# fail_test TESTNAME
70# incr_ntest
71#
72# Command run at the end of each test file:
73#
74# finish_test
75#
dan430e74c2010-06-07 17:47:26 +000076# Commands to help create test files that run with the "WAL" and other
77# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000078#
79# wal_is_wal_mode
80# wal_set_journal_mode ?DB?
81# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000082# permutation
dancb79e512010-08-06 13:50:07 +000083# presql
danc1a60c52010-06-07 14:28:16 +000084#
dan17c08232015-06-09 15:58:28 +000085# 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#
drh348784e2000-05-29 20:41:49 +000091
mistachkin1d406e02013-08-29 01:09:14 +000092# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000093# configure SQLite to take database file locks on the page that begins
94# 64KB into the database file instead of the one 1GB in. This means
95# the code that handles that special case can be tested without creating
96# very large database files.
97#
drh92febd92004-08-20 18:34:20 +000098set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000099sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +0000100
danc1a60c52010-06-07 14:28:16 +0000101
mistachkin1d406e02013-08-29 01:09:14 +0000102# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000103# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000104#
danc1a60c52010-06-07 14:28:16 +0000105# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000106#
danc1a60c52010-06-07 14:28:16 +0000107# becomes
drh4a50aac2007-08-23 02:47:53 +0000108#
danc1a60c52010-06-07 14:28:16 +0000109# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000110#
dan430e74c2010-06-07 17:47:26 +0000111if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000112 rename sqlite3 sqlite_orig
113 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000114 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000115 # This command is opening a new database connection.
116 #
117 if {[info exists ::G(perm:sqlite3_args)]} {
118 set args [concat $args $::G(perm:sqlite3_args)]
119 }
dan68928b62010-06-22 13:46:43 +0000120 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000121 lappend args -key {xyzzy}
122 }
123
dan3ccd20a2010-06-09 19:01:02 +0000124 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000125 if {[info exists ::G(perm:presql)]} {
126 [lindex $args 0] eval $::G(perm:presql)
127 }
dan1ce1b4a2010-12-07 14:32:28 +0000128 if {[info exists ::G(perm:dbconfig)]} {
129 set ::dbhandle [lindex $args 0]
130 uplevel #0 $::G(perm:dbconfig)
131 }
dan9a23d262020-07-16 14:52:24 +0000132 [lindex $args 0] cache size 3
dan3ccd20a2010-06-09 19:01:02 +0000133 set res
dan430e74c2010-06-07 17:47:26 +0000134 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000135 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000136 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000137 #
138 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000139 }
drh9eb9e262004-02-11 02:18:05 +0000140 }
141}
142
mistachkinfda06be2011-08-02 00:57:34 +0000143proc getFileRetries {} {
144 if {![info exists ::G(file-retries)]} {
145 #
146 # NOTE: Return the default number of retries for [file] operations. A
147 # value of zero or less here means "disabled".
148 #
mistachkinc60941f2012-09-13 01:51:02 +0000149 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000150 }
151 return $::G(file-retries)
152}
153
154proc getFileRetryDelay {} {
155 if {![info exists ::G(file-retry-delay)]} {
156 #
157 # NOTE: Return the default number of milliseconds to wait when retrying
158 # failed [file] operations. A value of zero or less means "do not
159 # wait".
160 #
161 return 100; # TODO: Good default?
162 }
163 return $::G(file-retry-delay)
164}
165
mistachkinf8a78462012-03-08 20:00:36 +0000166# Return the string representing the name of the current directory. On
167# Windows, the result is "normalized" to whatever our parent command shell
168# is using to prevent case-mismatch issues.
169#
170proc get_pwd {} {
171 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000172 #
173 # NOTE: Cannot use [file normalize] here because it would alter the
174 # case of the result to what Tcl considers canonical, which would
175 # defeat the purpose of this procedure.
176 #
mistachkinf3ebea82021-01-18 19:27:56 +0000177 if {[info exists ::env(ComSpec)]} {
178 set comSpec $::env(ComSpec)
179 } else {
180 # NOTE: Hard-code the typical default value.
181 set comSpec {C:\Windows\system32\cmd.exe}
182 }
mistachkin533b8f62012-03-08 20:28:31 +0000183 return [string map [list \\ /] \
mistachkinf3ebea82021-01-18 19:27:56 +0000184 [string trim [exec -- $comSpec /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000185 } else {
186 return [pwd]
187 }
188}
189
mistachkinfda06be2011-08-02 00:57:34 +0000190# Copy file $from into $to. This is used because some versions of
191# TCL for windows (notably the 8.4.1 binary package shipped with the
192# current mingw release) have a broken "file copy" command.
193#
194proc copy_file {from to} {
195 do_copy_file false $from $to
196}
197
198proc forcecopy {from to} {
199 do_copy_file true $from $to
200}
201
202proc do_copy_file {force from to} {
203 set nRetry [getFileRetries] ;# Maximum number of retries.
204 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
205
206 # On windows, sometimes even a [file copy -force] can fail. The cause is
207 # usually "tag-alongs" - programs like anti-virus software, automatic backup
208 # tools and various explorer extensions that keep a file open a little longer
209 # than we expect, causing the delete to fail.
210 #
211 # The solution is to wait a short amount of time before retrying the copy.
212 #
213 if {$nRetry > 0} {
214 for {set i 0} {$i<$nRetry} {incr i} {
215 set rc [catch {
216 if {$force} {
217 file copy -force $from $to
218 } else {
219 file copy $from $to
220 }
221 } msg]
222 if {$rc==0} break
223 if {$nDelay > 0} { after $nDelay }
224 }
225 if {$rc} { error $msg }
226 } else {
227 if {$force} {
228 file copy -force $from $to
229 } else {
230 file copy $from $to
231 }
232 }
233}
234
mistachkinc5484652012-03-05 22:52:33 +0000235# Check if a file name is relative
236#
237proc is_relative_file { file } {
238 return [expr {[file pathtype $file] != "absolute"}]
239}
240
mistachkin4a41f342012-03-06 03:00:49 +0000241# If the VFS supports using the current directory, returns [pwd];
242# otherwise, it returns only the provided suffix string (which is
243# empty by default).
244#
245proc test_pwd { args } {
246 if {[llength $args] > 0} {
247 set suffix1 [lindex $args 0]
248 if {[llength $args] > 1} {
249 set suffix2 [lindex $args 1]
250 } else {
251 set suffix2 $suffix1
252 }
253 } else {
254 set suffix1 ""; set suffix2 ""
255 }
256 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000257 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000258 } else {
259 return $suffix2
260 }
261}
262
mistachkinfda06be2011-08-02 00:57:34 +0000263# Delete a file or directory
264#
265proc delete_file {args} {
266 do_delete_file false {*}$args
267}
268
269proc forcedelete {args} {
270 do_delete_file true {*}$args
271}
272
273proc do_delete_file {force args} {
274 set nRetry [getFileRetries] ;# Maximum number of retries.
275 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
276
277 foreach filename $args {
278 # On windows, sometimes even a [file delete -force] can fail just after
279 # a file is closed. The cause is usually "tag-alongs" - programs like
280 # anti-virus software, automatic backup tools and various explorer
281 # extensions that keep a file open a little longer than we expect, causing
282 # the delete to fail.
283 #
284 # The solution is to wait a short amount of time before retrying the
285 # delete.
286 #
287 if {$nRetry > 0} {
288 for {set i 0} {$i<$nRetry} {incr i} {
289 set rc [catch {
290 if {$force} {
291 file delete -force $filename
292 } else {
293 file delete $filename
294 }
295 } msg]
296 if {$rc==0} break
297 if {$nDelay > 0} { after $nDelay }
298 }
299 if {$rc} { error $msg }
300 } else {
301 if {$force} {
302 file delete -force $filename
303 } else {
304 file delete $filename
305 }
306 }
307 }
308}
309
mistachkin1d406e02013-08-29 01:09:14 +0000310if {$::tcl_platform(platform) eq "windows"} {
311 proc do_remove_win32_dir {args} {
312 set nRetry [getFileRetries] ;# Maximum number of retries.
313 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
314
315 foreach dirName $args {
316 # On windows, sometimes even a [remove_win32_dir] can fail just after
317 # a directory is emptied. The cause is usually "tag-alongs" - programs
318 # like anti-virus software, automatic backup tools and various explorer
319 # extensions that keep a file open a little longer than we expect,
320 # causing the delete to fail.
321 #
322 # The solution is to wait a short amount of time before retrying the
323 # removal.
324 #
325 if {$nRetry > 0} {
326 for {set i 0} {$i < $nRetry} {incr i} {
327 set rc [catch {
328 remove_win32_dir $dirName
329 } msg]
330 if {$rc == 0} break
331 if {$nDelay > 0} { after $nDelay }
332 }
333 if {$rc} { error $msg }
334 } else {
335 remove_win32_dir $dirName
336 }
337 }
338 }
339
340 proc do_delete_win32_file {args} {
341 set nRetry [getFileRetries] ;# Maximum number of retries.
342 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
343
344 foreach fileName $args {
345 # On windows, sometimes even a [delete_win32_file] can fail just after
346 # a file is closed. The cause is usually "tag-alongs" - programs like
347 # anti-virus software, automatic backup tools and various explorer
348 # extensions that keep a file open a little longer than we expect,
349 # causing the delete to fail.
350 #
351 # The solution is to wait a short amount of time before retrying the
352 # delete.
353 #
354 if {$nRetry > 0} {
355 for {set i 0} {$i < $nRetry} {incr i} {
356 set rc [catch {
357 delete_win32_file $fileName
358 } msg]
359 if {$rc == 0} break
360 if {$nDelay > 0} { after $nDelay }
361 }
362 if {$rc} { error $msg }
363 } else {
364 delete_win32_file $fileName
365 }
366 }
367 }
368}
369
dancb354602010-07-08 09:44:42 +0000370proc execpresql {handle args} {
371 trace remove execution $handle enter [list execpresql $handle]
372 if {[info exists ::G(perm:presql)]} {
373 $handle eval $::G(perm:presql)
374 }
375}
376
dan68928b62010-06-22 13:46:43 +0000377# This command should be called after loading tester.tcl from within
378# all test scripts that are incompatible with encryption codecs.
379#
380proc do_not_use_codec {} {
381 set ::do_not_use_codec 1
382 reset_db
383}
drh422dded2016-07-25 16:10:43 +0000384unset -nocomplain do_not_use_codec
dan68928b62010-06-22 13:46:43 +0000385
drhaf3906a2016-03-14 17:05:04 +0000386# Return true if the "reserved_bytes" integer on database files is non-zero.
387#
388proc nonzero_reserved_bytes {} {
389 return [sqlite3 -has-codec]
390}
391
drh4b7b1c92016-02-15 19:38:17 +0000392# Print a HELP message and exit
393#
394proc print_help_and_quit {} {
395 puts {Options:
396 --pause Wait for user input before continuing
397 --soft-heap-limit=N Set the soft-heap-limit to N
drh31999c52019-11-14 17:46:32 +0000398 --hard-heap-limit=N Set the hard-heap-limit to N
drh4b7b1c92016-02-15 19:38:17 +0000399 --maxerror=N Quit after N errors
400 --verbose=(0|1) Control the amount of output. Default '1'
401 --output=FILE set --verbose=2 and output to FILE. Implies -q
402 -q Shorthand for --verbose=0
403 --help This message
404}
405 exit 1
406}
407
dan430e74c2010-06-07 17:47:26 +0000408# The following block only runs the first time this file is sourced. It
409# does not run in slave interpreters (since the ::cmdlinearg array is
410# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000411#
danc1a60c52010-06-07 14:28:16 +0000412if {[info exists cmdlinearg]==0} {
413
mistachkin1d406e02013-08-29 01:09:14 +0000414 # Parse any options specified in the $argv array. This script accepts the
415 # following options:
danc1a60c52010-06-07 14:28:16 +0000416 #
417 # --pause
418 # --soft-heap-limit=NN
drh31999c52019-11-14 17:46:32 +0000419 # --hard-heap-limit=NN
danc1a60c52010-06-07 14:28:16 +0000420 # --maxerror=NN
421 # --malloctrace=N
422 # --backtrace=N
423 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000424 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000425 # --file-retries=N
426 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000427 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000428 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000429 # --verbose=$val
430 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000431 # -q Reduce output
432 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000433 # --help
danc1a60c52010-06-07 14:28:16 +0000434 #
435 set cmdlinearg(soft-heap-limit) 0
drh31999c52019-11-14 17:46:32 +0000436 set cmdlinearg(hard-heap-limit) 0
danc1a60c52010-06-07 14:28:16 +0000437 set cmdlinearg(maxerror) 1000
438 set cmdlinearg(malloctrace) 0
439 set cmdlinearg(backtrace) 10
440 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000441 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000442 set cmdlinearg(file-retries) 0
443 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000444 set cmdlinearg(start) ""
445 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000446 set cmdlinearg(verbose) ""
447 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000448 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000449
450 set leftover [list]
451 foreach a $argv {
452 switch -regexp -- $a {
453 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000454 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000455 # opportunity to connect profiling tools to the process.
456 puts -nonewline "Press RETURN to begin..."
457 flush stdout
458 gets stdin
459 }
460 {^-+soft-heap-limit=.+$} {
461 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
462 }
drh31999c52019-11-14 17:46:32 +0000463 {^-+hard-heap-limit=.+$} {
464 foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break
465 }
danc1a60c52010-06-07 14:28:16 +0000466 {^-+maxerror=.+$} {
467 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
468 }
469 {^-+malloctrace=.+$} {
470 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
471 if {$cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +0000472 if {0==$::sqlite_options(memdebug)} {
473 set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build"
474 puts stderr $err
475 exit 1
476 }
danc1a60c52010-06-07 14:28:16 +0000477 sqlite3_memdebug_log start
478 }
479 }
480 {^-+backtrace=.+$} {
481 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan2a86c192017-01-25 17:44:13 +0000482 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danc1a60c52010-06-07 14:28:16 +0000483 }
danc1a60c52010-06-07 14:28:16 +0000484 {^-+binarylog=.+$} {
485 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000486 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000487 }
dan0626dfc2010-06-15 06:56:37 +0000488 {^-+soak=.+$} {
489 foreach {dummy cmdlinearg(soak)} [split $a =] break
490 set ::G(issoak) $cmdlinearg(soak)
491 }
mistachkinfda06be2011-08-02 00:57:34 +0000492 {^-+file-retries=.+$} {
493 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
494 set ::G(file-retries) $cmdlinearg(file-retries)
495 }
496 {^-+file-retry-delay=.+$} {
497 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
498 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
499 }
dan6a64d672011-04-04 15:38:16 +0000500 {^-+start=.+$} {
501 foreach {dummy cmdlinearg(start)} [split $a =] break
502
503 set ::G(start:file) $cmdlinearg(start)
504 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
505 set ::G(start:permutation) ${s.perm}
506 set ::G(start:file) ${s.file}
507 }
508 if {$::G(start:file) == ""} {unset ::G(start:file)}
509 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000510 {^-+match=.+$} {
511 foreach {dummy cmdlinearg(match)} [split $a =] break
512
513 set ::G(match) $cmdlinearg(match)
514 if {$::G(match) == ""} {unset ::G(match)}
515 }
dan17c08232015-06-09 15:58:28 +0000516
517 {^-+output=.+$} {
518 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000519 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000520 if {$cmdlinearg(verbose)==""} {
521 set cmdlinearg(verbose) 2
522 }
523 }
524 {^-+verbose=.+$} {
525 foreach {dummy cmdlinearg(verbose)} [split $a =] break
526 if {$cmdlinearg(verbose)=="file"} {
527 set cmdlinearg(verbose) 2
528 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
529 error "option --verbose= must be set to a boolean or to \"file\""
530 }
531 }
drhd5704a82016-03-14 13:42:29 +0000532 {^-+testdir=.*$} {
533 foreach {dummy cmdlinearg(testdir)} [split $a =] break
534 }
drh4b7b1c92016-02-15 19:38:17 +0000535 {.*help.*} {
536 print_help_and_quit
537 }
538 {^-q$} {
539 set cmdlinearg(output) test-out.txt
540 set cmdlinearg(verbose) 2
541 }
dan17c08232015-06-09 15:58:28 +0000542
danc1a60c52010-06-07 14:28:16 +0000543 default {
dan40cf36f2016-04-30 19:23:10 +0000544 if {[file tail $a]==$a} {
545 lappend leftover $a
546 } else {
547 lappend leftover [file normalize $a]
548 }
danc1a60c52010-06-07 14:28:16 +0000549 }
550 }
551 }
drhbea14132016-03-14 14:28:43 +0000552 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000553 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000554 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000555 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000556 if {$cmdlinearg(testdir)!=""} {
557 file mkdir $cmdlinearg(testdir)
558 cd $cmdlinearg(testdir)
559 }
danc1a60c52010-06-07 14:28:16 +0000560 set argv $leftover
561
dan430e74c2010-06-07 17:47:26 +0000562 # Install the malloc layer used to inject OOM errors. And the 'automatic'
563 # extensions. This only needs to be done once for the process.
564 #
mistachkin1d406e02013-08-29 01:09:14 +0000565 sqlite3_shutdown
566 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000567 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000568 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000569
dan430e74c2010-06-07 17:47:26 +0000570 # If the --binarylog option was specified, create the logging VFS. This
571 # call installs the new VFS as the default for all SQLite connections.
572 #
danc1a60c52010-06-07 14:28:16 +0000573 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000574 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000575 }
576
577 # Set the backtrace depth, if malloc tracing is enabled.
578 #
579 if {$cmdlinearg(malloctrace)} {
580 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000581 }
dan17c08232015-06-09 15:58:28 +0000582
583 if {$cmdlinearg(output)!=""} {
584 puts "Copying output to file $cmdlinearg(output)"
585 set ::G(output_fd) [open $cmdlinearg(output) w]
586 fconfigure $::G(output_fd) -buffering line
587 }
588
589 if {$cmdlinearg(verbose)==""} {
590 set cmdlinearg(verbose) 1
591 }
dan1d07f1d2019-04-01 17:24:20 +0000592
593 if {[info commands vdbe_coverage]!=""} {
594 vdbe_coverage start
595 }
danielk1977f7b9d662008-06-23 18:49:43 +0000596}
danielk197703ba3fa2009-01-09 10:49:14 +0000597
danc1a60c52010-06-07 14:28:16 +0000598# Update the soft-heap-limit each time this script is run. In that
599# way if an individual test file changes the soft-heap-limit, it
600# will be reset at the start of the next test file.
601#
drh31999c52019-11-14 17:46:32 +0000602sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit)
603sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit)
danc1a60c52010-06-07 14:28:16 +0000604
605# Create a test database
606#
danielk197703ba3fa2009-01-09 10:49:14 +0000607proc reset_db {} {
608 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000609 forcedelete test.db
610 forcedelete test.db-journal
611 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000612 sqlite3 db ./test.db
613 set ::DB [sqlite3_connection_pointer db]
614 if {[info exists ::SETUP_SQL]} {
615 db eval $::SETUP_SQL
616 }
drhcd61c282002-03-06 22:01:34 +0000617}
danielk197703ba3fa2009-01-09 10:49:14 +0000618reset_db
drhbec2bf42000-05-29 23:48:22 +0000619
620# Abort early if this script has been run before.
621#
danc1a60c52010-06-07 14:28:16 +0000622if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000623
drhce2198c2010-09-10 13:22:59 +0000624# Make sure memory statistics are enabled.
625#
626sqlite3_config_memstatus 1
627
danc1a60c52010-06-07 14:28:16 +0000628# Initialize the test counters and set up commands to access them.
629# Or, if this is a slave interpreter, set up aliases to write the
630# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000631#
danc1a60c52010-06-07 14:28:16 +0000632if {0==[info exists ::SLAVE]} {
633 set TC(errors) 0
634 set TC(count) 0
635 set TC(fail_list) [list]
636 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000637 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000638
639 proc set_test_counter {counter args} {
640 if {[llength $args]} {
641 set ::TC($counter) [lindex $args 0]
642 }
643 set ::TC($counter)
644 }
drhb62c3352006-11-23 09:39:16 +0000645}
drh348784e2000-05-29 20:41:49 +0000646
drh521cc842008-04-15 02:36:33 +0000647# Record the fact that a sequence of tests were omitted.
648#
mistachkin6c3c1a02011-11-12 03:17:40 +0000649proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000650 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000651 if {$append} {
652 lappend omitList [list $name $reason]
653 }
danc1a60c52010-06-07 14:28:16 +0000654 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000655}
656
danc1a60c52010-06-07 14:28:16 +0000657# Record the fact that a test failed.
658#
659proc fail_test {name} {
660 set f [set_test_counter fail_list]
661 lappend f $name
662 set_test_counter fail_list $f
663 set_test_counter errors [expr [set_test_counter errors] + 1]
664
665 set nFail [set_test_counter errors]
666 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000667 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000668 finalize_testing
669 }
670}
671
drhcca17c32013-04-19 12:32:52 +0000672# Remember a warning message to be displayed at the conclusion of all testing
673#
674proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000675 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000676 set warnList [set_test_counter warn_list]
677 if {$append} {
678 lappend warnList $msg
679 }
680 set_test_counter warn_list $warnList
681}
682
683
danc1a60c52010-06-07 14:28:16 +0000684# Increment the number of tests run
685#
686proc incr_ntest {} {
687 set_test_counter count [expr [set_test_counter count] + 1]
688}
689
dan17c08232015-06-09 15:58:28 +0000690# Return true if --verbose=1 was specified on the command line. Otherwise,
691# return false.
692#
693proc verbose {} {
694 return $::cmdlinearg(verbose)
695}
696
697# Use the following commands instead of [puts] for test output within
698# this file. Test scripts can still use regular [puts], which is directed
699# to stdout and, if one is open, the --output file.
700#
701# output1: output that should be printed if --verbose=1 was specified.
702# output2: output that should be printed unconditionally.
703# output2_if_no_verbose: output that should be printed only if --verbose=0.
704#
705proc output1 {args} {
706 set v [verbose]
707 if {$v==1} {
708 uplevel output2 $args
709 } elseif {$v==2} {
710 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
711 }
712}
713proc output2 {args} {
714 set nArg [llength $args]
715 uplevel puts $args
716}
717proc output2_if_no_verbose {args} {
718 set v [verbose]
719 if {$v==0} {
720 uplevel output2 $args
721 } elseif {$v==2} {
722 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
723 }
724}
725
726# Override the [puts] command so that if no channel is explicitly
727# specified the string is written to both stdout and to the file
728# specified by "--output=", if any.
729#
730proc puts_override {args} {
731 set nArg [llength $args]
732 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
733 uplevel puts_original $args
734 if {[info exists ::G(output_fd)]} {
735 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
736 }
737 } else {
738 # A channel was explicitly specified.
739 uplevel puts_original $args
740 }
741}
742rename puts puts_original
743proc puts {args} { uplevel puts_override $args }
744
danc1a60c52010-06-07 14:28:16 +0000745
mistachkin1d406e02013-08-29 01:09:14 +0000746# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000747#
drhe39cd912016-07-09 17:47:01 +0000748# The $expected parameter is the expected result. The result is the return
749# value from the last TCL command in $cmd.
750#
751# Normally, $expected must match exactly. But if $expected is of the form
752# "/regexp/" then regular expression matching is used. If $expected is
753# "~/regexp/" then the regular expression must NOT match. If $expected is
754# of the form "#/value-list/" then each term in value-list must be numeric
755# and must approximately match the corresponding numeric term in $result.
756# Values must match within 10%. Or if the $expected term is A..B then the
757# $result term must be in between A and B.
758#
drh348784e2000-05-29 20:41:49 +0000759proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000760 global argv cmdlinearg
761
dan8c408002010-11-01 17:38:24 +0000762 fix_testname name
763
drh4a50aac2007-08-23 02:47:53 +0000764 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000765
mistachkin1d406e02013-08-29 01:09:14 +0000766# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000767# set go 1
768# } else {
769# set go 0
770# foreach pattern $argv {
771# if {[string match $pattern $name]} {
772# set go 1
773# break
774# }
775# }
776# }
dan430e74c2010-06-07 17:47:26 +0000777
dand506de02010-07-03 13:59:01 +0000778 if {[info exists ::G(perm:prefix)]} {
779 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000780 }
781
danc1a60c52010-06-07 14:28:16 +0000782 incr_ntest
dan17c08232015-06-09 15:58:28 +0000783 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000784 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000785
786 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
787 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000788 output2_if_no_verbose -nonewline $name...
789 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000790 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000791 } else {
dan00bd55e2020-03-20 20:54:28 +0000792 if {[permutation]=="maindbname"} {
793 set result [string map [list [string tolower ICECUBE] main] $result]
794 }
drhe39cd912016-07-09 17:47:01 +0000795 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000796 # "expected" is of the form "/PATTERN/" then the result if correct if
797 # regular expression PATTERN matches the result. "~/PATTERN/" means
798 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000799 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000800 set re [string range $expected 2 end-1]
801 if {[string index $re 0]=="*"} {
802 # If the regular expression begins with * then treat it as a glob instead
803 set ok [string match $re $result]
804 } else {
805 set re [string map {# {[-0-9.]+}} $re]
806 set ok [regexp $re $result]
807 }
808 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000809 } elseif {[string index $expected 0]=="#"} {
810 # Numeric range value comparison. Each term of the $result is matched
811 # against one term of $expect. Both $result and $expected terms must be
812 # numeric. The values must match within 10%. Or if $expected is of the
813 # form A..B then the $result term must be between A and B.
814 set e2 [string range $expected 2 end-1]
815 foreach i $result j $e2 {
816 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
817 set ok [expr {$i+0>=$A && $i+0<=$B}]
818 } else {
819 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
820 }
821 if {!$ok} break
822 }
823 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000824 } else {
drhc2b23e72013-11-13 15:32:15 +0000825 set re [string range $expected 1 end-1]
826 if {[string index $re 0]=="*"} {
827 # If the regular expression begins with * then treat it as a glob instead
828 set ok [string match $re $result]
829 } else {
830 set re [string map {# {[-0-9.]+}} $re]
831 set ok [regexp $re $result]
832 }
drh3f17aef2012-04-27 01:08:02 +0000833 }
drh70bdcc72013-05-30 19:28:34 +0000834 } elseif {[regexp {^~?\*.*\*$} $expected]} {
835 # "expected" is of the form "*GLOB*" then the result if correct if
836 # glob pattern GLOB matches the result. "~/GLOB/" means
837 # the glob must not match.
838 if {[string index $expected 0]=="~"} {
839 set e [string range $expected 1 end]
840 set ok [expr {![string match $e $result]}]
841 } else {
842 set ok [string match $expected $result]
843 }
drh3f17aef2012-04-27 01:08:02 +0000844 } else {
845 set ok [expr {[string compare $result $expected]==0}]
846 }
847 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000848 # if {![info exists ::testprefix] || $::testprefix eq ""} {
849 # error "no test prefix"
850 # }
drhd66b2e02015-11-12 21:42:40 +0000851 output1 ""
drh061d35c2015-11-13 00:03:14 +0000852 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000853 fail_test $name
854 } else {
dan17c08232015-06-09 15:58:28 +0000855 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000856 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000857 }
drh348784e2000-05-29 20:41:49 +0000858 } else {
dan17c08232015-06-09 15:58:28 +0000859 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000860 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000861 }
drh6558db82007-04-13 03:23:21 +0000862 flush stdout
drh348784e2000-05-29 20:41:49 +0000863}
dana3f51082010-09-30 18:43:14 +0000864
mistachkinf21979d2015-01-18 05:35:01 +0000865proc dumpbytes {s} {
866 set r ""
867 for {set i 0} {$i < [string length $s]} {incr i} {
868 if {$i > 0} {append r " "}
869 append r [format %02X [scan [string index $s $i] %c]]
870 }
871 return $r
872}
873
drh8df91852012-04-24 12:46:05 +0000874proc catchcmd {db {cmd ""}} {
875 global CLI
876 set out [open cmds.txt w]
877 puts $out $cmd
878 close $out
879 set line "exec $CLI $db < cmds.txt"
880 set rc [catch { eval $line } msg]
881 list $rc $msg
882}
883
mistachkinf21979d2015-01-18 05:35:01 +0000884proc catchcmdex {db {cmd ""}} {
885 global CLI
886 set out [open cmds.txt w]
887 fconfigure $out -encoding binary -translation binary
888 puts -nonewline $out $cmd
889 close $out
890 set line "exec -keepnewline -- $CLI $db < cmds.txt"
891 set chans [list stdin stdout stderr]
892 foreach chan $chans {
893 catch {
894 set modes($chan) [fconfigure $chan]
895 fconfigure $chan -encoding binary -translation binary -buffering none
896 }
897 }
898 set rc [catch { eval $line } msg]
899 foreach chan $chans {
900 catch {
901 eval fconfigure [list $chan] $modes($chan)
902 }
903 }
904 # puts [dumpbytes $msg]
905 list $rc $msg
906}
907
shaneh55505172011-06-21 19:30:19 +0000908proc filepath_normalize {p} {
909 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000910 if {$::tcl_platform(platform)!="unix"} {
mistachkin8c333cf2021-02-03 19:38:40 +0000911 string map [list \\ / \{/ / .db\} .db] \
912 [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]
shanehc4896402011-06-21 19:38:16 +0000913 } {
914 set p
shaneh55505172011-06-21 19:30:19 +0000915 }
916}
917proc do_filepath_test {name cmd expected} {
918 uplevel [list do_test $name [
919 subst -nocommands { filepath_normalize [ $cmd ] }
920 ] [filepath_normalize $expected]]
921}
922
dan33f53792011-05-05 19:44:22 +0000923proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000924 # different TCL versions display floating point values differently.
925 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000926}
927proc do_realnum_test {name cmd expected} {
928 uplevel [list do_test $name [
929 subst -nocommands { realnum_normalize [ $cmd ] }
930 ] [realnum_normalize $expected]]
931}
932
dana3f51082010-09-30 18:43:14 +0000933proc fix_testname {varname} {
934 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000935 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000936 && [string is digit [string range $testname 0 0]]
937 } {
938 set testname "${::testprefix}-$testname"
939 }
940}
dan67340072011-04-16 19:23:10 +0000941
942proc normalize_list {L} {
943 set L2 [list]
944 foreach l $L {lappend L2 $l}
945 set L2
946}
drh5f9742e2013-08-29 15:08:38 +0000947
danff677b22017-02-04 17:33:30 +0000948# Either:
949#
950# do_execsql_test TESTNAME SQL ?RES?
951# do_execsql_test -db DB TESTNAME SQL ?RES?
952#
953proc do_execsql_test {args} {
954 set db db
955 if {[lindex $args 0]=="-db"} {
956 set db [lindex $args 1]
957 set args [lrange $args 2 end]
958 }
959
960 if {[llength $args]==2} {
961 foreach {testname sql} $args {}
962 set result ""
963 } elseif {[llength $args]==3} {
964 foreach {testname sql result} $args {}
dan9274ad82019-01-14 19:13:30 +0000965
966 # With some versions of Tcl on windows, if $result is all whitespace but
967 # contains some CR/LF characters, the [list {*}$result] below returns a
968 # copy of $result instead of a zero length string. Not clear exactly why
969 # this is. The following is a workaround.
970 if {[llength $result]==0} { set result "" }
danff677b22017-02-04 17:33:30 +0000971 } else {
972 error [string trim {
973 wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?"
974 }]
975 }
976
dana3f51082010-09-30 18:43:14 +0000977 fix_testname testname
danff677b22017-02-04 17:33:30 +0000978
979 uplevel do_test \
980 [list $testname] \
981 [list "execsql {$sql} $db"] \
982 [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000983}
danff677b22017-02-04 17:33:30 +0000984
dan153eda02010-06-21 07:45:47 +0000985proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000986 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000987 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000988}
drh3c2aeae2014-01-24 14:37:44 +0000989proc do_timed_execsql_test {testname sql {result {}}} {
990 fix_testname testname
991 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
992 [list [list {*}$result]]
993}
drh03c39052018-05-02 14:24:34 +0000994
995# Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output
996# as an ASCII-art graph and return a string that is that graph.
997#
998# Hexadecimal literals in the output text are converted into "xxxxxx" since those
999# literals are pointer values that might very from one run of the test to the
1000# next, yet we want the output to be consistent.
1001#
1002proc query_plan_graph {sql} {
1003 db eval "EXPLAIN QUERY PLAN $sql" {
1004 set dx($id) $detail
1005 lappend cx($parent) $id
1006 }
1007 set a "\n QUERY PLAN\n"
1008 append a [append_graph " " dx cx 0]
drhfef37762018-07-10 19:48:35 +00001009 regsub -all { 0x[A-F0-9]+\y} $a { xxxxxx} a
1010 regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a
1011 return $a
dan39854792010-11-15 16:12:58 +00001012}
dan153eda02010-06-21 07:45:47 +00001013
drh03c39052018-05-02 14:24:34 +00001014# Helper routine for [query_plan_graph SQL]:
1015#
1016# Output rows of the graph that are children of $level.
1017#
1018# prefix: Prepend to every output line
1019#
1020# dxname: Name of an array variable that stores text describe
1021# The description for $id is $dx($id)
1022#
1023# cxname: Name of an array variable holding children of item.
1024# Children of $id are $cx($id)
1025#
1026# level: Render all lines that are children of $level
1027#
1028proc append_graph {prefix dxname cxname level} {
1029 upvar $dxname dx $cxname cx
1030 set a ""
1031 set x $cx($level)
1032 set n [llength $x]
1033 for {set i 0} {$i<$n} {incr i} {
1034 set id [lindex $x $i]
1035 if {$i==$n-1} {
1036 set p1 "`--"
1037 set p2 " "
1038 } else {
1039 set p1 "|--"
1040 set p2 "| "
1041 }
1042 append a $prefix$p1$dx($id)\n
1043 if {[info exists cx($id)]} {
1044 append a [append_graph "$prefix$p2" dx cx $id]
1045 }
1046 }
1047 return $a
1048}
1049
1050# Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res
1051#
1052# If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the
1053# complete graph which must match the output of [query_plan_graph $sql]
1054# exactly.
1055#
1056# If $res does not begin with "\s+QUERY PLAN\n" then take it is a string
1057# that must be found somewhere in the query plan output.
1058#
1059proc do_eqp_test {name sql res} {
1060 if {[regexp {^\s+QUERY PLAN\n} $res]} {
1061 uplevel do_test $name [list [list query_plan_graph $sql]] [list $res]
1062 } else {
1063 if {[string index $res 0]!="/"} {
1064 set res "/*$res*/"
1065 }
1066 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
1067 }
1068}
1069
1070
dan51f06982010-09-18 19:00:12 +00001071#-------------------------------------------------------------------------
1072# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
1073#
1074# Where switches are:
1075#
1076# -errorformat FMTSTRING
1077# -count
danaf1dcab2010-09-20 19:17:53 +00001078# -query SQL
dana16d1062010-09-28 17:37:28 +00001079# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +00001080# -repair TCL
dan51f06982010-09-18 19:00:12 +00001081#
1082proc do_select_tests {prefix args} {
1083
1084 set testlist [lindex $args end]
1085 set switches [lrange $args 0 end-1]
1086
1087 set errfmt ""
1088 set countonly 0
dana16d1062010-09-28 17:37:28 +00001089 set tclquery ""
danaf7626f2010-09-23 18:47:36 +00001090 set repair ""
dan51f06982010-09-18 19:00:12 +00001091
1092 for {set i 0} {$i < [llength $switches]} {incr i} {
1093 set s [lindex $switches $i]
1094 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +00001095 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +00001096 set tclquery [list execsql [lindex $switches [incr i]]]
1097 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
1098 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +00001099 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +00001100 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +00001101 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
1102 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +00001103 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
1104 set countonly 1
1105 } else {
1106 error "unknown switch: $s"
1107 }
1108 }
1109
1110 if {$countonly && $errfmt!=""} {
1111 error "Cannot use -count and -errorformat together"
1112 }
1113 set nTestlist [llength $testlist]
1114 if {$nTestlist%3 || $nTestlist==0 } {
1115 error "SELECT test list contains [llength $testlist] elements"
1116 }
1117
danaf7626f2010-09-23 18:47:36 +00001118 eval $repair
dan51f06982010-09-18 19:00:12 +00001119 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +00001120 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +00001121 execsql $sql
dana16d1062010-09-28 17:37:28 +00001122 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
1123 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +00001124 set nRow 0
1125 db eval $sql {incr nRow}
1126 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
1127 } elseif {$errfmt==""} {
1128 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
1129 } else {
1130 set res [list 1 [string trim [format $errfmt {*}$res]]]
1131 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
1132 }
danaf7626f2010-09-23 18:47:36 +00001133 eval $repair
dan51f06982010-09-18 19:00:12 +00001134 }
danaf7626f2010-09-23 18:47:36 +00001135
dan51f06982010-09-18 19:00:12 +00001136}
1137
danb04757a2010-09-21 16:59:16 +00001138proc delete_all_data {} {
1139 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1140 db eval "DELETE FROM '[string map {' ''} $t]'"
1141 }
1142}
drh348784e2000-05-29 20:41:49 +00001143
mistachkin1d406e02013-08-29 01:09:14 +00001144# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001145# Return the number of microseconds per statement.
1146#
drh3590f152006-11-23 21:09:10 +00001147proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001148 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001149 flush stdout
1150 set speed [time {sqlite3_exec_nr db $sql}]
1151 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001152 if {$tm == 0} {
1153 set rate [format %20s "many"]
1154 } else {
1155 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1156 }
drh3590f152006-11-23 21:09:10 +00001157 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001158 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001159 global total_time
1160 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001161 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001162}
drh45c236d2008-03-22 01:08:00 +00001163proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001164 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001165 flush stdout
1166 set speed [time {eval $script}]
1167 set tm [lindex $speed 0]
1168 if {$tm == 0} {
1169 set rate [format %20s "many"]
1170 } else {
1171 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1172 }
1173 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001174 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001175 global total_time
1176 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001177 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001178}
drhdd924312007-03-31 22:34:16 +00001179proc speed_trial_init {name} {
1180 global total_time
1181 set total_time 0
dana975b592010-10-08 16:09:43 +00001182 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001183 sqlite3 versdb :memory:
1184 set vers [versdb one {SELECT sqlite_source_id()}]
1185 versdb close
dan17c08232015-06-09 15:58:28 +00001186 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001187}
1188proc speed_trial_summary {name} {
1189 global total_time
dan17c08232015-06-09 15:58:28 +00001190 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001191
1192 if { 0 } {
1193 sqlite3 versdb :memory:
1194 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1195 versdb close
dan17c08232015-06-09 15:58:28 +00001196 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001197 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001198 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001199 }
1200 }
drhb62c3352006-11-23 09:39:16 +00001201}
1202
drh348784e2000-05-29 20:41:49 +00001203# Run this routine last
1204#
1205proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001206 catch {db close}
dane8559832014-07-31 17:35:40 +00001207 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001208 catch {db2 close}
1209 catch {db3 close}
1210 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001211}
1212proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001213 global sqlite_open_file_count
1214
1215 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001216
drh6e142f52000-06-08 13:36:40 +00001217 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001218 catch {db2 close}
1219 catch {db3 close}
1220
drh9bc54492007-10-23 14:49:59 +00001221 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001222 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001223 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001224 db close
drh984bfaa2008-03-19 16:08:53 +00001225 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001226
drh31999c52019-11-14 17:46:32 +00001227 sqlite3_soft_heap_limit64 0
1228 sqlite3_hard_heap_limit64 0
danc1a60c52010-06-07 14:28:16 +00001229 set nTest [incr_ntest]
1230 set nErr [set_test_counter errors]
1231
drhef866372013-05-22 20:49:02 +00001232 set nKnown 0
1233 if {[file readable known-problems.txt]} {
1234 set fd [open known-problems.txt]
1235 set content [read $fd]
1236 close $fd
1237 foreach x $content {set known_error($x) 1}
1238 foreach x [set_test_counter fail_list] {
1239 if {[info exists known_error($x)]} {incr nKnown}
1240 }
1241 }
1242 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001243 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001244 out of $nTest tests"
1245 } else {
drh72bf6a32016-01-07 02:06:55 +00001246 set cpuinfo {}
1247 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1248 append cpuinfo " $::tcl_platform(os)"
1249 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1250 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1251 output2 "SQLite [sqlite3 -sourceid]"
1252 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001253 }
1254 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001255 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001256 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001257 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001258 }
dan17c08232015-06-09 15:58:28 +00001259 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001260 }
drhcca17c32013-04-19 12:32:52 +00001261 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001262 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001263 }
danielk1977ee8b7992009-03-26 17:13:06 +00001264 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001265 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001266 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001267 set prec {}
1268 foreach {rec} [lsort $omitList] {
1269 if {$rec==$prec} continue
1270 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001271 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001272 }
1273 }
drh80788d82006-09-02 14:50:23 +00001274 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001275 output2 "******************************************************************"
1276 output2 "N.B.: The version of TCL that you used to build this test harness"
1277 output2 "is defective in that it does not support 64-bit integers. Some or"
1278 output2 "all of the test failures above might be a result from this defect"
1279 output2 "in your TCL build."
1280 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001281 }
danc1a60c52010-06-07 14:28:16 +00001282 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001283 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001284 }
drh94e92032003-02-16 22:21:32 +00001285 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001286 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001287 incr nErr
1288 }
drheafc43b2010-07-26 18:43:40 +00001289 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1290 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001291 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001292 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001293 incr nErr
dan253c6ee2018-09-18 17:00:06 +00001294 ifcapable mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001295 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001296 sqlite3_memdebug_dump ./memleak.txt
1297 }
drhf3a65f72007-08-22 20:18:21 +00001298 } else {
dan17c08232015-06-09 15:58:28 +00001299 output2 "All memory allocations freed - no leaks"
dan253c6ee2018-09-18 17:00:06 +00001300 ifcapable mem5 {
drhd2bb3272007-10-15 19:34:32 +00001301 sqlite3_memdebug_dump ./memusage.txt
1302 }
drhf3a65f72007-08-22 20:18:21 +00001303 }
drhf7141992008-06-19 00:16:08 +00001304 show_memstats
dan17c08232015-06-09 15:58:28 +00001305 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1306 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001307 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001308 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001309 }
danc1a60c52010-06-07 14:28:16 +00001310 if {$::cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +00001311 output2 "Writing mallocs.tcl..."
1312 memdebug_log_sql mallocs.tcl
danielk197735754ac2008-03-21 17:29:37 +00001313 sqlite3_memdebug_log stop
1314 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001315 if {[sqlite3_memory_used]>0} {
dan253c6ee2018-09-18 17:00:06 +00001316 output2 "Writing leaks.tcl..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001317 sqlite3_memdebug_log sync
dan253c6ee2018-09-18 17:00:06 +00001318 memdebug_log_sql leaks.tcl
danielk1977dbdc4d42008-03-28 07:42:53 +00001319 }
danielk197735754ac2008-03-21 17:29:37 +00001320 }
dan1d07f1d2019-04-01 17:24:20 +00001321 if {[info commands vdbe_coverage]!=""} {
1322 vdbe_coverage_report
1323 }
drhd9910fe2006-10-04 11:55:49 +00001324 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001325 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001326 }
1327 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001328 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001329 }
drhdb25e382001-03-15 18:21:22 +00001330 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001331}
1332
dan1d07f1d2019-04-01 17:24:20 +00001333proc vdbe_coverage_report {} {
1334 puts "Writing vdbe coverage report to vdbe_coverage.txt"
1335 set lSrc [list]
1336 set iLine 0
1337 if {[file exists ../sqlite3.c]} {
1338 set fd [open ../sqlite3.c]
1339 set iLine
1340 while { ![eof $fd] } {
1341 set line [gets $fd]
1342 incr iLine
1343 if {[regexp {^/\** Begin file (.*\.c) \**/} $line -> file]} {
1344 lappend lSrc [list $iLine $file]
1345 }
1346 }
1347 close $fd
1348 }
1349 set fd [open vdbe_coverage.txt w]
1350 foreach miss [vdbe_coverage report] {
danafb3f3c2019-04-01 18:43:09 +00001351 foreach {line branch never} $miss {}
dan1d07f1d2019-04-01 17:24:20 +00001352 set nextfile ""
1353 while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} {
1354 set nextfile [lindex $lSrc 0 1]
1355 set lSrc [lrange $lSrc 1 end]
1356 }
1357 if {$nextfile != ""} {
1358 puts $fd ""
1359 puts $fd "### $nextfile ###"
1360 }
danafb3f3c2019-04-01 18:43:09 +00001361 puts $fd "Vdbe branch $line: never $never (path $branch)"
dan1d07f1d2019-04-01 17:24:20 +00001362 }
1363 close $fd
1364}
1365
drhf7141992008-06-19 00:16:08 +00001366# Display memory statistics for analysis and debugging purposes.
1367#
1368proc show_memstats {} {
1369 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001370 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1371 set val [format {now %10d max %10d max-size %10d} \
1372 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001373 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001374 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1375 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001376 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001377 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001378 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1379 set val [format {now %10d max %10d max-size %10d} \
1380 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001381 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001382 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1383 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001384 output1 "Page-cache overflow: $val"
drhec424a52008-07-25 15:39:03 +00001385 ifcapable yytrackmaxstackdepth {
1386 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1387 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001388 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001389 }
drhf7141992008-06-19 00:16:08 +00001390}
1391
drh348784e2000-05-29 20:41:49 +00001392# A procedure to execute SQL
1393#
drhc4a3c772001-04-04 11:48:57 +00001394proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001395 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001396 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001397}
drh3c2aeae2014-01-24 14:37:44 +00001398proc execsql_timed {sql {db db}} {
1399 set tm [time {
1400 set x [uplevel [list $db eval $sql]]
1401 } 1]
1402 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001403 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001404 set x
1405}
drh3aadb2e2000-05-31 17:59:25 +00001406
drhadbca9c2001-09-27 15:11:53 +00001407# Execute SQL and catch exceptions.
1408#
1409proc catchsql {sql {db db}} {
1410 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001411 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001412 lappend r $msg
1413 return $r
1414}
1415
drh04096482001-11-09 22:41:44 +00001416# Do an VDBE code dump on the SQL given
1417#
1418proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001419 output2 ""
1420 output2 "addr opcode p1 p2 p3 p4 p5 #"
1421 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001422 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001423 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001424 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1425 ]
drh04096482001-11-09 22:41:44 +00001426 }
1427}
1428
dane83267d2013-11-05 14:19:22 +00001429proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001430 output2 ""
1431 output2 "addr opcode p1 p2 p3 p4 p5 #"
1432 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001433
dane83267d2013-11-05 14:19:22 +00001434
dan5c663c32013-11-06 14:52:40 +00001435 # Set up colors for the different opcodes. Scheme is as follows:
1436 #
1437 # Red: Opcodes that write to a b-tree.
1438 # Blue: Opcodes that reposition or seek a cursor.
1439 # Green: The ResultRow opcode.
1440 #
dandea63f22014-03-03 16:48:47 +00001441 if { [catch {fconfigure stdout -mode}]==0 } {
1442 set R "\033\[31;1m" ;# Red fg
1443 set G "\033\[32;1m" ;# Green fg
1444 set B "\033\[34;1m" ;# Red fg
1445 set D "\033\[39;0m" ;# Default fg
1446 } else {
1447 set R ""
1448 set G ""
1449 set B ""
1450 set D ""
1451 }
dan5c663c32013-11-06 14:52:40 +00001452 foreach opcode {
dan8da209b2016-07-26 18:06:08 +00001453 Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001454 NoConflict Next Prev VNext VPrev VFilter
dan8da209b2016-07-26 18:06:08 +00001455 SorterSort SorterNext NextIfOpen
dan5c663c32013-11-06 14:52:40 +00001456 } {
1457 set color($opcode) $B
1458 }
1459 foreach opcode {ResultRow} {
1460 set color($opcode) $G
1461 }
1462 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1463 set color($opcode) $R
1464 }
1465
dan427ebba2013-11-05 16:39:31 +00001466 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001467 $db eval "explain $sql" {} {
1468 set x($addr) 0
1469 set op($addr) $opcode
1470
dan427ebba2013-11-05 16:39:31 +00001471 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1472 set linebreak($p2) 1
1473 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001474 }
1475
dan8da209b2016-07-26 18:06:08 +00001476 if {$opcode=="Once"} {
1477 for {set i $addr} {$i<$p2} {incr i} {
1478 set star($i) $addr
1479 }
1480 }
1481
dane3ab7292013-11-12 12:30:09 +00001482 if {$opcode=="Next" || $opcode=="Prev"
1483 || $opcode=="VNext" || $opcode=="VPrev"
dan8da209b2016-07-26 18:06:08 +00001484 || $opcode=="SorterNext" || $opcode=="NextIfOpen"
dane3ab7292013-11-12 12:30:09 +00001485 } {
dane83267d2013-11-05 14:19:22 +00001486 for {set i $p2} {$i<$addr} {incr i} {
1487 incr x($i) 2
1488 }
1489 }
1490
1491 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1492 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1493 incr x($i) 2
1494 }
1495 }
dan427ebba2013-11-05 16:39:31 +00001496
1497 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1498 set linebreak([expr $addr+1]) 1
1499 }
dane83267d2013-11-05 14:19:22 +00001500 }
1501
1502 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001503 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001504 output2 ""
dane83267d2013-11-05 14:19:22 +00001505 }
1506 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001507
dan8da209b2016-07-26 18:06:08 +00001508 if {[info exists star($addr)]} {
1509 set ii [expr $x($star($addr))]
1510 append I " "
1511 set I [string replace $I $ii $ii *]
1512 }
1513
dan5c663c32013-11-06 14:52:40 +00001514 set col ""
1515 catch { set col $color($opcode) }
1516
mistachkineeb31ff2015-06-10 23:02:38 +00001517 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001518 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001519 ]
dane83267d2013-11-05 14:19:22 +00001520 }
mistachkineeb31ff2015-06-10 23:02:38 +00001521 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001522}
1523
drhdafc0ce2008-04-17 19:14:02 +00001524# Show the VDBE program for an SQL statement but omit the Trace
1525# opcode at the beginning. This procedure can be used to prove
1526# that different SQL statements generate exactly the same VDBE code.
1527#
1528proc explain_no_trace {sql} {
1529 set tr [db eval "EXPLAIN $sql"]
1530 return [lrange $tr 7 end]
1531}
1532
drh3aadb2e2000-05-31 17:59:25 +00001533# Another procedure to execute SQL. This one includes the field
1534# names in the returned list.
1535#
1536proc execsql2 {sql} {
1537 set result {}
1538 db eval $sql data {
1539 foreach f $data(*) {
1540 lappend result $f $data($f)
1541 }
1542 }
1543 return $result
1544}
drh17a68932001-01-31 13:28:08 +00001545
mistachkin1d406e02013-08-29 01:09:14 +00001546# Use a temporary in-memory database to execute SQL statements
1547#
1548proc memdbsql {sql} {
1549 sqlite3 memdb :memory:
1550 set result [memdb eval $sql]
1551 memdb close
1552 return $result
1553}
1554
drhdde85d92003-03-01 19:45:34 +00001555# Use the non-callback API to execute multiple SQL statements
1556#
1557proc stepsql {dbptr sql} {
1558 set sql [string trim $sql]
1559 set r 0
1560 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001561 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001562 return [list 1 $vm]
1563 }
1564 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001565# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1566# foreach v $VAL {lappend r $v}
1567# }
1568 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1569 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1570 lappend r [sqlite3_column_text $vm $i]
1571 }
drhdde85d92003-03-01 19:45:34 +00001572 }
danielk1977106bb232004-05-21 10:08:53 +00001573 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001574 return [list 1 $errmsg]
1575 }
1576 }
1577 return $r
1578}
1579
drh21504322002-06-25 13:16:02 +00001580# Do an integrity check of the entire database
1581#
danielk197704103022009-02-03 16:51:24 +00001582proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001583 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001584 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001585 }
1586}
1587
drh433dccf2013-02-09 15:37:11 +00001588# Check the extended error code
1589#
1590proc verify_ex_errcode {name expected {db db}} {
1591 do_test $name [list sqlite3_extended_errcode $db] $expected
1592}
1593
danc6055c72011-04-28 18:46:46 +00001594
1595# Return true if the SQL statement passed as the second argument uses a
1596# statement transaction.
1597#
1598proc sql_uses_stmt {db sql} {
1599 set stmt [sqlite3_prepare $db $sql -1 dummy]
1600 set uses [uses_stmt_journal $stmt]
1601 sqlite3_finalize $stmt
1602 return $uses
1603}
1604
danielk1977db4e8862008-01-16 08:24:46 +00001605proc fix_ifcapable_expr {expr} {
1606 set ret ""
1607 set state 0
1608 for {set i 0} {$i < [string length $expr]} {incr i} {
1609 set char [string range $expr $i $i]
1610 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1611 if {$newstate && !$state} {
1612 append ret {$::sqlite_options(}
1613 }
1614 if {!$newstate && $state} {
1615 append ret )
1616 }
1617 append ret $char
1618 set state $newstate
1619 }
1620 if {$state} {append ret )}
1621 return $ret
1622}
1623
mistachkinc60941f2012-09-13 01:51:02 +00001624# Returns non-zero if the capabilities are present; zero otherwise.
1625#
1626proc capable {expr} {
1627 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1628}
1629
drh27d258a2004-10-30 20:23:09 +00001630# Evaluate a boolean expression of capabilities. If true, execute the
1631# code. Omit the code if false.
1632#
danielk19773e8c37e2005-01-21 03:12:14 +00001633proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001634 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1635 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001636 if ($e2) {
1637 set c [catch {uplevel 1 $code} r]
1638 } else {
1639 set c [catch {uplevel 1 $elsecode} r]
1640 }
1641 return -code $c $r
drh21504322002-06-25 13:16:02 +00001642}
danielk197745901d62004-11-10 15:27:38 +00001643
danielk1977aca790a2005-01-13 11:07:52 +00001644# This proc execs a seperate process that crashes midway through executing
1645# the SQL script $sql on database test.db.
1646#
1647# The crash occurs during a sync() of file $crashfile. When the crash
1648# occurs a random subset of all unsynced writes made by the process are
1649# written into the files on disk. Argument $crashdelay indicates the
1650# number of file syncs to wait before crashing.
1651#
1652# The return value is a list of two elements. The first element is a
1653# boolean, indicating whether or not the process actually crashed or
1654# reported some other error. The second element in the returned list is the
1655# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001656# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001657#
danielk1977f8940ae2007-08-23 11:07:10 +00001658# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001659#
1660proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001661
1662 set blocksize ""
1663 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001664 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001665 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001666 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001667 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001668 set dc ""
dancb1b0a62017-03-02 14:51:47 +00001669 set dfltvfs 0
danielk197759a33f92007-03-17 10:26:59 +00001670 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001671
danielk197759a33f92007-03-17 10:26:59 +00001672 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1673 set z [lindex $args $ii]
1674 set n [string length $z]
1675 set z2 [lindex $args [expr $ii+1]]
1676
1677 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001678 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001679 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001680 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001681 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001682 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
dancb1b0a62017-03-02 14:51:47 +00001683 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }\
1684 elseif {$n>1 && [string first $z -dfltvfs]==0} {set dfltvfs $z2 }\
danielk197759a33f92007-03-17 10:26:59 +00001685 else { error "Unrecognized option: $z" }
1686 }
1687
1688 if {$crashfile eq ""} {
1689 error "Compulsory option -file missing"
1690 }
1691
mistachkin1d406e02013-08-29 01:09:14 +00001692 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001693 # cfSync(), which can be different then what TCL uses by
1694 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001695 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001696
1697 set f [open crash.tcl w]
dan5cb960b2021-01-07 16:29:34 +00001698 puts $f "sqlite3_initialize ; sqlite3_shutdown"
1699 puts $f "catch { install_malloc_faultsim 1 }"
dancb1b0a62017-03-02 14:51:47 +00001700 puts $f "sqlite3_crash_enable 1 $dfltvfs"
danielk1977f8940ae2007-08-23 11:07:10 +00001701 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001702 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
dan5cb960b2021-01-07 16:29:34 +00001703 puts $f "autoinstall_test_functions"
danielk1977933bbd62007-03-17 07:22:42 +00001704
1705 # This block sets the cache size of the main database to 10
1706 # pages. This is done in case the build is configured to omit
1707 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001708 if {$opendb!=""} {
1709 puts $f $opendb
1710 puts $f {db eval {SELECT * FROM sqlite_master;}}
1711 puts $f {set bt [btree_from_db db]}
1712 puts $f {btree_set_cache_size $bt 10}
1713 }
dan999cd082013-12-09 20:42:03 +00001714
drhcb1f0f62008-01-08 15:18:52 +00001715 if {$prngseed} {
1716 set seed [expr {$prngseed%10007+1}]
1717 # puts seed=$seed
1718 puts $f "db eval {SELECT randomblob($seed)}"
1719 }
danielk1977933bbd62007-03-17 07:22:42 +00001720
drhf8587402008-01-08 16:03:49 +00001721 if {[string length $tclbody]>0} {
1722 puts $f $tclbody
1723 }
1724 if {[string length $sql]>0} {
1725 puts $f "db eval {"
1726 puts $f "$sql"
1727 puts $f "}"
1728 }
danielk1977aca790a2005-01-13 11:07:52 +00001729 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001730 set r [catch {
dan5cb960b2021-01-07 16:29:34 +00001731 exec [info nameofexec] crash.tcl >@stdout 2>@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001732 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001733
shanehf2c08822010-07-08 16:30:44 +00001734 # Windows/ActiveState TCL returns a slightly different
1735 # error message. We map that to the expected message
1736 # so that we don't have to change all of the test
1737 # cases.
1738 if {$::tcl_platform(platform)=="windows"} {
1739 if {$msg=="child killed: unknown signal"} {
1740 set msg "child process exited abnormally"
1741 }
1742 }
dan39acaec2020-11-23 15:30:16 +00001743 if {$r && [string match {*ERROR: LeakSanitizer*} $msg]} {
1744 set msg "child process exited abnormally"
1745 }
mistachkin1d406e02013-08-29 01:09:14 +00001746
danielk1977aca790a2005-01-13 11:07:52 +00001747 lappend r $msg
1748}
1749
dan33447e72017-07-22 20:12:31 +00001750# crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL
1751#
1752proc crash_on_write {args} {
1753
1754 set nArg [llength $args]
1755 if {$nArg<2 || $nArg%2} {
1756 error "bad args: $args"
1757 }
1758 set zSql [lindex $args end]
1759 set nDelay [lindex $args end-1]
1760
1761 set devchar {}
1762 for {set ii 0} {$ii < $nArg-2} {incr ii 2} {
1763 set opt [lindex $args $ii]
1764 switch -- [lindex $args $ii] {
1765 -devchar {
1766 set devchar [lindex $args [expr $ii+1]]
1767 }
1768
1769 default { error "unrecognized option: $opt" }
1770 }
1771 }
1772
1773 set f [open crash.tcl w]
1774 puts $f "sqlite3_crash_on_write $nDelay"
1775 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
1776 puts $f "sqlite3 db test.db -vfs writecrash"
1777 puts $f "db eval {$zSql}"
1778 puts $f "set {} {}"
1779
1780 close $f
1781 set r [catch {
1782 exec [info nameofexec] crash.tcl >@stdout
1783 } msg]
1784
1785 # Windows/ActiveState TCL returns a slightly different
1786 # error message. We map that to the expected message
1787 # so that we don't have to change all of the test
1788 # cases.
1789 if {$::tcl_platform(platform)=="windows"} {
1790 if {$msg=="child killed: unknown signal"} {
1791 set msg "child process exited abnormally"
1792 }
1793 }
1794
1795 lappend r $msg
1796}
1797
danb88e24f2013-02-23 18:58:11 +00001798proc run_ioerr_prep {} {
1799 set ::sqlite_io_error_pending 0
1800 catch {db close}
1801 catch {db2 close}
1802 catch {forcedelete test.db}
1803 catch {forcedelete test.db-journal}
1804 catch {forcedelete test2.db}
1805 catch {forcedelete test2.db-journal}
1806 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1807 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1808 if {[info exists ::ioerropts(-tclprep)]} {
1809 eval $::ioerropts(-tclprep)
1810 }
1811 if {[info exists ::ioerropts(-sqlprep)]} {
1812 execsql $::ioerropts(-sqlprep)
1813 }
1814 expr 0
1815}
1816
danielk197732554c12005-01-22 03:39:39 +00001817# Usage: do_ioerr_test <test number> <options...>
1818#
1819# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001820# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001821# used to name the tests executed by this proc. Options are as follows:
1822#
1823# -tclprep TCL script to run to prepare test.
1824# -sqlprep SQL script to run to prepare test.
1825# -tclbody TCL script to run with IO error simulation.
1826# -sqlbody TCL script to run with IO error simulation.
1827# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001828# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001829# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001830# -start Value of 'N' to begin with (default 1)
1831#
1832# -cksum Boolean. If true, test that the database does
1833# not change during the execution of the test case.
1834#
1835proc do_ioerr_test {testname args} {
1836
danielk197732554c12005-01-22 03:39:39 +00001837 set ::ioerropts(-start) 1
1838 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001839 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001840 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001841 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001842 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001843 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001844 array set ::ioerropts $args
1845
danielk197747cd39c2008-05-12 12:41:15 +00001846 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1847 # a couple of obscure IO errors that do not return them.
1848 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001849
danb88e24f2013-02-23 18:58:11 +00001850 # Create a single TCL script from the TCL and SQL specified
1851 # as the body of the test.
1852 set ::ioerrorbody {}
1853 if {[info exists ::ioerropts(-tclbody)]} {
1854 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1855 }
1856 if {[info exists ::ioerropts(-sqlbody)]} {
1857 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1858 }
1859
1860 save_prng_state
1861 if {$::ioerropts(-cksum)} {
1862 run_ioerr_prep
1863 eval $::ioerrorbody
1864 set ::goodcksum [cksum]
1865 }
danielk197747cd39c2008-05-12 12:41:15 +00001866
danielk197732554c12005-01-22 03:39:39 +00001867 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001868 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001869 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001870 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001871 incr ::ioerropts(-count) -1
1872 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001873
danielk197732554c12005-01-22 03:39:39 +00001874 # Skip this IO error if it was specified with the "-exclude" option.
1875 if {[info exists ::ioerropts(-exclude)]} {
1876 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1877 }
danielk1977861f7452008-06-05 11:39:11 +00001878 if {$::ioerropts(-restoreprng)} {
1879 restore_prng_state
1880 }
danielk197732554c12005-01-22 03:39:39 +00001881
mistachkin1d406e02013-08-29 01:09:14 +00001882 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001883 # SQL (in that order) to prepare for the test case.
1884 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001885 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001886 } {0}
1887
1888 # Read the 'checksum' of the database.
1889 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001890 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001891 }
drh93aed5a2008-01-16 17:46:38 +00001892
danielk197732554c12005-01-22 03:39:39 +00001893 # Set the Nth IO error to fail.
1894 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001895 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001896 set ::sqlite_io_error_pending $n
1897 }] $n
danielk197732554c12005-01-22 03:39:39 +00001898
danb88e24f2013-02-23 18:58:11 +00001899 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001900 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001901 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001902 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001903 set ::sqlite_io_error_hit 0
1904 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001905 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001906 set ::errseen $r
dan20235e52021-07-14 21:18:31 +00001907 if {[info commands db]!=""} {
1908 set rc [sqlite3_errcode db]
1909 if {$::ioerropts(-erc)} {
1910 # If we are in extended result code mode, make sure all of the
1911 # IOERRs we get back really do have their extended code values.
1912 # If an extended result code is returned, the sqlite3_errcode
1913 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1914 # where nnnn is a number
1915 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1916 return $rc
1917 }
1918 } else {
1919 # If we are not in extended result code mode, make sure no
1920 # extended error codes are returned.
1921 if {[regexp {\+\d} $rc]} {
1922 return $rc
1923 }
drhe49f9822006-09-15 12:29:16 +00001924 }
1925 }
drh93aed5a2008-01-16 17:46:38 +00001926 # The test repeats as long as $::go is non-zero. $::go starts out
1927 # as 1. When a test runs to completion without hitting an I/O
1928 # error, that means there is no point in continuing with this test
1929 # case so set $::go to zero.
1930 #
1931 if {$::sqlite_io_error_pending>0} {
1932 set ::go 0
1933 set q 0
1934 set ::sqlite_io_error_pending 0
1935 } else {
1936 set q 1
1937 }
1938
drhc9ac5ca2005-11-04 22:03:30 +00001939 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001940 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1941 set r 1
1942 }
danielk1977f2fa8312006-01-24 13:09:33 +00001943 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001944
1945 # One of two things must have happened. either
1946 # 1. We never hit the IO error and the SQL returned OK
1947 # 2. An IO error was hit and the SQL failed
1948 #
dand41a29a2010-05-06 15:56:28 +00001949 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001950 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001951 } {1}
1952
danielk197780daec62008-05-12 10:57:02 +00001953 set ::sqlite_io_error_hit 0
1954 set ::sqlite_io_error_pending 0
1955
mistachkin1d406e02013-08-29 01:09:14 +00001956 # Check that no page references were leaked. There should be
1957 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001958 # or zero otherwise.
1959 #
danielk197781620542008-06-07 05:19:37 +00001960 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001961 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001962 # occurs while attempting to detect a hot-journal file), then
1963 # there may 0 page references and an active transaction according
1964 # to [sqlite3_get_autocommit].
1965 #
danielk19774abd5442008-05-05 15:26:50 +00001966 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001967 do_test $testname.$n.4 {
1968 set bt [btree_from_db db]
1969 db_enter db
1970 array set stats [btree_pager_stats $bt]
1971 db_leave db
danielk197781620542008-06-07 05:19:37 +00001972 set nRef $stats(ref)
1973 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1974 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001975 }
1976
mistachkin1d406e02013-08-29 01:09:14 +00001977 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001978 # and the pager is not running in exclusive-locking mode,
1979 # check that the pager is in "unlocked" state. Theoretically,
1980 # if a call to xUnlock() failed due to an IO error the underlying
1981 # file may still be locked.
1982 #
1983 ifcapable pragma {
1984 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001985 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001986 && [db one {pragma locking_mode}] eq "normal"
1987 && [sqlite3_get_autocommit db]
1988 } {
1989 do_test $testname.$n.5 {
1990 set bt [btree_from_db db]
1991 db_enter db
1992 array set stats [btree_pager_stats $bt]
1993 db_leave db
1994 set stats(state)
1995 } 0
1996 }
1997 }
1998
mistachkin48864df2013-03-21 21:20:32 +00001999 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00002000 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00002001 #
drh1aa5af12008-03-07 19:51:14 +00002002 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00002003 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00002004 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00002005 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00002006 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00002007 set nowcksum [cksum]
2008 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
2009 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00002010 output2 "now=$nowcksum"
2011 output2 "the=$::checksum"
2012 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00002013 }
2014 set res
2015 } 1
danielk197732554c12005-01-22 03:39:39 +00002016 }
2017
drh1aa5af12008-03-07 19:51:14 +00002018 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00002019 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00002020 if {[info exists ::ioerropts(-cleanup)]} {
2021 catch $::ioerropts(-cleanup)
2022 }
danielk197732554c12005-01-22 03:39:39 +00002023 }
2024 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00002025 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00002026 unset ::ioerropts
2027}
2028
drh93aed5a2008-01-16 17:46:38 +00002029# Return a checksum based on the contents of the main database associated
2030# with connection $db
danielk197732554c12005-01-22 03:39:39 +00002031#
2032proc cksum {{db db}} {
2033 set txt [$db eval {
2034 SELECT name, type, sql FROM sqlite_master order by name
2035 }]\n
2036 foreach tbl [$db eval {
2037 SELECT name FROM sqlite_master WHERE type='table' order by name
2038 }] {
2039 append txt [$db eval "SELECT * FROM $tbl"]\n
2040 }
2041 foreach prag {default_synchronous default_cache_size} {
2042 append txt $prag-[$db eval "PRAGMA $prag"]\n
2043 }
2044 set cksum [string length $txt]-[md5 $txt]
2045 # puts $cksum-[file size test.db]
2046 return $cksum
2047}
2048
drh93aed5a2008-01-16 17:46:38 +00002049# Generate a checksum based on the contents of the main and temp tables
2050# database $db. If the checksum of two databases is the same, and the
2051# integrity-check passes for both, the two databases are identical.
2052#
drh1db639c2008-01-17 02:36:28 +00002053proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00002054 set ret [list]
2055 ifcapable tempdb {
2056 set sql {
2057 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2058 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
2059 SELECT 'sqlite_master' UNION
2060 SELECT 'sqlite_temp_master' ORDER BY 1
2061 }
2062 } else {
2063 set sql {
2064 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2065 SELECT 'sqlite_master' ORDER BY 1
2066 }
2067 }
2068 set tbllist [$db eval $sql]
2069 set txt {}
2070 foreach tbl $tbllist {
2071 append txt [$db eval "SELECT * FROM $tbl"]
2072 }
2073 foreach prag {default_cache_size} {
2074 append txt $prag-[$db eval "PRAGMA $prag"]\n
2075 }
2076 # puts txt=$txt
2077 return [md5 $txt]
2078}
2079
drhdc2c4912009-02-04 22:46:47 +00002080# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00002081# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00002082# Examples of $dbname are "temp" or "main".
2083#
2084proc dbcksum {db dbname} {
2085 if {$dbname=="temp"} {
2086 set master sqlite_temp_master
2087 } else {
2088 set master $dbname.sqlite_master
2089 }
2090 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
2091 set txt [$db eval "SELECT * FROM $master"]\n
2092 foreach tab $alltab {
2093 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
2094 }
2095 return [md5 $txt]
2096}
2097
dan253c6ee2018-09-18 17:00:06 +00002098proc memdebug_log_sql {filename} {
danielk197735754ac2008-03-21 17:29:37 +00002099
danielk19776f332c12008-03-21 14:22:44 +00002100 set data [sqlite3_memdebug_log dump]
2101 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00002102 if {$nFrame < 0} { return "" }
2103
danielk197735754ac2008-03-21 17:29:37 +00002104 set database temp
2105
danielk19776ab3a2e2009-02-19 14:39:25 +00002106 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00002107
2108 set sql ""
2109 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00002110 set nCall [lindex $e 0]
2111 set nByte [lindex $e 1]
2112 set lStack [lrange $e 2 end]
2113 append sql "INSERT INTO ${database}.malloc VALUES"
2114 append sql "('test', $nCall, $nByte, '$lStack');\n"
2115 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00002116 set frames($f) 1
2117 }
2118 }
2119
2120 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00002121 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00002122
dandee9be92019-02-05 16:53:26 +00002123 set pid [pid]
2124
danielk19776f332c12008-03-21 14:22:44 +00002125 foreach f [array names frames] {
2126 set addr [format %x $f]
dandee9be92019-02-05 16:53:26 +00002127 set cmd "eu-addr2line --pid=$pid $addr"
danielk19776f332c12008-03-21 14:22:44 +00002128 set line [eval exec $cmd]
2129 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00002130
2131 set file [lindex [split $line :] 0]
2132 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00002133 }
2134
danielk197735754ac2008-03-21 17:29:37 +00002135 foreach f [array names files] {
2136 set contents ""
2137 catch {
2138 set fd [open $f]
2139 set contents [read $fd]
2140 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002141 }
danielk197735754ac2008-03-21 17:29:37 +00002142 set contents [string map {' ''} $contents]
2143 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00002144 }
danielk19776f332c12008-03-21 14:22:44 +00002145
dan253c6ee2018-09-18 17:00:06 +00002146 set escaped "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
2147 set escaped [string map [list "{" "\\{" "}" "\\}"] $escaped]
2148
danielk197735754ac2008-03-21 17:29:37 +00002149 set fd [open $filename w]
dan253c6ee2018-09-18 17:00:06 +00002150 puts $fd "set BUILTIN {"
2151 puts $fd $escaped
2152 puts $fd "}"
2153 puts $fd {set BUILTIN [string map [list "\\{" "{" "\\}" "}"] $BUILTIN]}
2154 set mtv [open $::testdir/malloctraceviewer.tcl]
2155 set txt [read $mtv]
2156 close $mtv
2157 puts $fd $txt
danielk197735754ac2008-03-21 17:29:37 +00002158 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002159}
drh93aed5a2008-01-16 17:46:38 +00002160
danf5894502009-10-07 18:41:19 +00002161# Drop all tables in database [db]
2162proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00002163 ifcapable trigger&&foreignkey {
2164 set pk [$db one "PRAGMA foreign_keys"]
2165 $db eval "PRAGMA foreign_keys = OFF"
2166 }
drh9a6ffc82010-02-15 18:03:20 +00002167 foreach {idx name file} [db eval {PRAGMA database_list}] {
2168 if {$idx==1} {
2169 set master sqlite_temp_master
2170 } else {
2171 set master $name.sqlite_master
2172 }
2173 foreach {t type} [$db eval "
2174 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00002175 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00002176 "] {
dana16d1062010-09-28 17:37:28 +00002177 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00002178 }
danf5894502009-10-07 18:41:19 +00002179 }
shaneh4e7b32f2009-12-17 22:12:51 +00002180 ifcapable trigger&&foreignkey {
2181 $db eval "PRAGMA foreign_keys = $pk"
2182 }
danf5894502009-10-07 18:41:19 +00002183}
2184
dand05a7142016-08-02 17:07:51 +00002185# Drop all auxiliary indexes from the main database opened by handle [db].
2186#
2187proc drop_all_indexes {{db db}} {
2188 set L [$db eval {
2189 SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%'
2190 }]
2191 foreach idx $L { $db eval "DROP INDEX $idx" }
2192}
2193
2194
dan71cb5182010-04-26 12:39:03 +00002195#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00002196# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00002197# "wal", then the tests are run in WAL mode. Otherwise, they should be run
2198# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00002199# intrusive:
dan71cb5182010-04-26 12:39:03 +00002200#
2201# wal_set_journal_mode ?DB?
2202#
2203# If running a WAL test, execute "PRAGMA journal_mode = wal" using
2204# connection handle DB. Otherwise, this command is a no-op.
2205#
2206# wal_check_journal_mode TESTNAME ?DB?
2207#
2208# If running a WAL test, execute a tests case that fails if the main
2209# database for connection handle DB is not currently a WAL database.
2210# Otherwise (if not running a WAL permutation) this is a no-op.
2211#
2212# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00002213#
dan71cb5182010-04-26 12:39:03 +00002214# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00002215#
dan71cb5182010-04-26 12:39:03 +00002216proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00002217 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00002218}
2219proc wal_set_journal_mode {{db db}} {
2220 if { [wal_is_wal_mode] } {
2221 $db eval "PRAGMA journal_mode = WAL"
2222 }
2223}
2224proc wal_check_journal_mode {testname {db db}} {
2225 if { [wal_is_wal_mode] } {
2226 $db eval { SELECT * FROM sqlite_master }
2227 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
2228 }
2229}
2230
dan05accd22016-04-27 18:54:49 +00002231proc wal_is_capable {} {
2232 ifcapable !wal { return 0 }
2233 if {[permutation]=="journaltest"} { return 0 }
2234 return 1
2235}
2236
dan430e74c2010-06-07 17:47:26 +00002237proc permutation {} {
2238 set perm ""
2239 catch {set perm $::G(perm:name)}
2240 set perm
2241}
dancb79e512010-08-06 13:50:07 +00002242proc presql {} {
2243 set presql ""
2244 catch {set presql $::G(perm:presql)}
2245 set presql
2246}
dan430e74c2010-06-07 17:47:26 +00002247
danbe7721d2016-02-04 17:31:03 +00002248proc isquick {} {
2249 set ret 0
2250 catch {set ret $::G(isquick)}
2251 set ret
2252}
2253
danc1a60c52010-06-07 14:28:16 +00002254#-------------------------------------------------------------------------
2255#
2256proc slave_test_script {script} {
2257
2258 # Create the interpreter used to run the test script.
2259 interp create tinterp
2260
2261 # Populate some global variables that tester.tcl expects to see.
2262 foreach {var value} [list \
2263 ::argv0 $::argv0 \
2264 ::argv {} \
2265 ::SLAVE 1 \
2266 ] {
2267 interp eval tinterp [list set $var $value]
2268 }
2269
dan17c08232015-06-09 15:58:28 +00002270 # If output is being copied into a file, share the file-descriptor with
2271 # the interpreter.
2272 if {[info exists ::G(output_fd)]} {
2273 interp share {} $::G(output_fd) tinterp
2274 }
2275
danc1a60c52010-06-07 14:28:16 +00002276 # The alias used to access the global test counters.
2277 tinterp alias set_test_counter set_test_counter
2278
2279 # Set up the ::cmdlinearg array in the slave.
2280 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2281
dan430e74c2010-06-07 17:47:26 +00002282 # Set up the ::G array in the slave.
2283 interp eval tinterp [list array set ::G [array get ::G]]
2284
danc1a60c52010-06-07 14:28:16 +00002285 # Load the various test interfaces implemented in C.
2286 load_testfixture_extensions tinterp
2287
2288 # Run the test script.
2289 interp eval tinterp $script
2290
danea5542d2010-07-06 11:26:15 +00002291 # Check if the interpreter call [run_thread_tests]
2292 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2293 set ::run_thread_tests_called 1
2294 }
2295
danc1a60c52010-06-07 14:28:16 +00002296 # Delete the interpreter used to run the test script.
2297 interp delete tinterp
2298}
2299
dan430e74c2010-06-07 17:47:26 +00002300proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002301 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002302
dan6a64d672011-04-04 15:38:16 +00002303 if {[info exists ::G(start:permutation)]} {
2304 if {[permutation] != $::G(start:permutation)} return
2305 unset ::G(start:permutation)
2306 }
2307 if {[info exists ::G(start:file)]} {
2308 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2309 unset ::G(start:file)
2310 }
2311
dan92d516a2010-07-05 14:54:48 +00002312 # Remember the value of the shared-cache setting. So that it is possible
2313 # to check afterwards that it was not modified by the test script.
2314 #
2315 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002316
dan92d516a2010-07-05 14:54:48 +00002317 # Run the test script in a slave interpreter.
2318 #
danea5542d2010-07-06 11:26:15 +00002319 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002320 reset_prng_state
2321 set ::sqlite_open_file_count 0
2322 set time [time { slave_test_script [list source $zFile] }]
2323 set ms [expr [lindex $time 0] / 1000]
2324
dan92d516a2010-07-05 14:54:48 +00002325 # Test that all files opened by the test script were closed. Omit this
2326 # if the test script has "thread" in its name. The open file counter
2327 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002328 #
danea5542d2010-07-06 11:26:15 +00002329 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002330 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2331 }
dan430e74c2010-06-07 17:47:26 +00002332 set ::sqlite_open_file_count 0
2333
mistachkin1d406e02013-08-29 01:09:14 +00002334 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002335 # the test script.
2336 #
mistachkin1d406e02013-08-29 01:09:14 +00002337 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002338 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2339 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2340 }
dan430e74c2010-06-07 17:47:26 +00002341
dan92d516a2010-07-05 14:54:48 +00002342 # Add some info to the output.
2343 #
dan17c08232015-06-09 15:58:28 +00002344 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002345 show_memstats
danc1a60c52010-06-07 14:28:16 +00002346}
2347
dan59257dc2010-08-04 11:34:31 +00002348# Open a new connection on database test.db and execute the SQL script
2349# supplied as an argument. Before returning, close the new conection and
2350# restore the 4 byte fields starting at header offsets 28, 92 and 96
2351# to the values they held before the SQL was executed. This simulates
2352# a write by a pre-3.7.0 client.
2353#
2354proc sql36231 {sql} {
2355 set B [hexio_read test.db 92 8]
2356 set A [hexio_read test.db 28 4]
2357 sqlite3 db36231 test.db
2358 catch { db36231 func a_string a_string }
2359 execsql $sql db36231
2360 db36231 close
2361 hexio_write test.db 28 $A
2362 hexio_write test.db 92 $B
2363 return ""
2364}
danf5894502009-10-07 18:41:19 +00002365
danccc7ff42010-11-29 12:06:45 +00002366proc db_save {} {
2367 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2368 foreach f [glob -nocomplain test.db*] {
2369 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002370 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002371 }
2372}
2373proc db_save_and_close {} {
2374 db_save
2375 catch { db close }
2376 return ""
2377}
2378proc db_restore {} {
2379 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2380 foreach f2 [glob -nocomplain sv_test.db*] {
2381 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002382 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002383 }
2384}
2385proc db_restore_and_reopen {{dbfile test.db}} {
2386 catch { db close }
2387 db_restore
2388 sqlite3 db $dbfile
2389}
2390proc db_delete_and_reopen {{file test.db}} {
2391 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002392 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002393 sqlite3 db $file
2394}
2395
dan03bc5252015-07-24 14:17:17 +00002396# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2397# to configure the size of the PAGECACHE allocation using the parameters
2398# provided to this command. Save the old PAGECACHE parameters in a global
2399# variable so that [test_restore_config_pagecache] can restore the previous
2400# configuration.
2401#
2402# Before returning, reopen connection [db] on file test.db.
2403#
2404proc test_set_config_pagecache {sz nPg} {
2405 catch {db close}
2406 catch {db2 close}
2407 catch {db3 close}
2408
2409 sqlite3_shutdown
2410 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2411 sqlite3_initialize
2412 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002413 reset_db
dan03bc5252015-07-24 14:17:17 +00002414}
2415
2416# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2417# to configure the size of the PAGECACHE allocation to the size saved in
2418# the global variable by an earlier call to [test_set_config_pagecache].
2419#
2420# Before returning, reopen connection [db] on file test.db.
2421#
2422proc test_restore_config_pagecache {} {
2423 catch {db close}
2424 catch {db2 close}
2425 catch {db3 close}
2426
2427 sqlite3_shutdown
2428 eval sqlite3_config_pagecache $::old_pagecache_config
2429 unset ::old_pagecache_config
2430 sqlite3_initialize
2431 autoinstall_test_functions
2432 sqlite3 db test.db
2433}
2434
dan43efc182017-12-19 17:42:13 +00002435proc test_binary_name {nm} {
dan089555c2016-03-15 09:55:44 +00002436 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002437 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002438 } else {
dan1e8dae02016-03-19 14:53:36 +00002439 set ret $nm
dan089555c2016-03-15 09:55:44 +00002440 }
dan43efc182017-12-19 17:42:13 +00002441 file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]
2442}
2443
2444proc test_find_binary {nm} {
2445 set ret [test_binary_name $nm]
dan089555c2016-03-15 09:55:44 +00002446 if {![file executable $ret]} {
2447 finish_test
dan49aed582016-03-19 15:13:59 +00002448 return ""
dan089555c2016-03-15 09:55:44 +00002449 }
2450 return $ret
2451}
2452
danielk197745901d62004-11-10 15:27:38 +00002453# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2454# the tests in shell[1-5].test. If no such executable can be found, invoke
2455# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002456#
dan64b95bb2012-05-12 05:30:29 +00002457proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002458 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002459 if {$prog==""} { return -code return }
2460 return $prog
drh348784e2000-05-29 20:41:49 +00002461}
2462
dan1e8dae02016-03-19 14:53:36 +00002463# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2464# the tests in sqldiff tests. If no such executable can be found, invoke
2465# [finish_test ; return] in the callers context.
2466#
2467proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002468 set prog [test_find_binary sqldiff]
2469 if {$prog==""} { return -code return }
2470 return $prog
dan1e8dae02016-03-19 14:53:36 +00002471}
2472
daneab0e102018-02-07 18:02:50 +00002473# Call sqlite3_expanded_sql() on all statements associated with database
2474# connection $db. This sometimes finds use-after-free bugs if run with
2475# valgrind or address-sanitizer.
2476proc expand_all_sql {db} {
2477 set stmt ""
2478 while {[set stmt [sqlite3_next_stmt $db $stmt]]!=""} {
2479 sqlite3_expanded_sql $stmt
2480 }
2481}
2482
dan1e8dae02016-03-19 14:53:36 +00002483
drh348784e2000-05-29 20:41:49 +00002484# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2485# to non-zero, then set the global variable $AUTOVACUUM to 1.
2486set AUTOVACUUM $sqlite_options(default_autovacuum)
2487
dan64b95bb2012-05-12 05:30:29 +00002488# Make sure the FTS enhanced query syntax is disabled.
2489set sqlite_fts3_enable_parentheses 0
2490
drh348784e2000-05-29 20:41:49 +00002491# During testing, assume that all database files are well-formed. The
2492# few test cases that deliberately corrupt database files should rescind
2493# this setting by invoking "database_can_be_corrupt"
2494#
2495database_never_corrupt
drhca439a42020-07-22 21:05:23 +00002496extra_schema_checks 1
drh348784e2000-05-29 20:41:49 +00002497
drh348784e2000-05-29 20:41:49 +00002498source $testdir/thread_common.tcl
2499source $testdir/malloc_common.tcl