blob: 05c52343f095a629169493a65e898bb46e86f3c2 [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 }
dan3ccd20a2010-06-09 19:01:02 +0000132 set res
dan430e74c2010-06-07 17:47:26 +0000133 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000134 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000135 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000136 #
137 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000138 }
drh9eb9e262004-02-11 02:18:05 +0000139 }
140}
141
mistachkinfda06be2011-08-02 00:57:34 +0000142proc getFileRetries {} {
143 if {![info exists ::G(file-retries)]} {
144 #
145 # NOTE: Return the default number of retries for [file] operations. A
146 # value of zero or less here means "disabled".
147 #
mistachkinc60941f2012-09-13 01:51:02 +0000148 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000149 }
150 return $::G(file-retries)
151}
152
153proc getFileRetryDelay {} {
154 if {![info exists ::G(file-retry-delay)]} {
155 #
156 # NOTE: Return the default number of milliseconds to wait when retrying
157 # failed [file] operations. A value of zero or less means "do not
158 # wait".
159 #
160 return 100; # TODO: Good default?
161 }
162 return $::G(file-retry-delay)
163}
164
mistachkinf8a78462012-03-08 20:00:36 +0000165# Return the string representing the name of the current directory. On
166# Windows, the result is "normalized" to whatever our parent command shell
167# is using to prevent case-mismatch issues.
168#
169proc get_pwd {} {
170 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000171 #
172 # NOTE: Cannot use [file normalize] here because it would alter the
173 # case of the result to what Tcl considers canonical, which would
174 # defeat the purpose of this procedure.
175 #
176 return [string map [list \\ /] \
177 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000178 } else {
179 return [pwd]
180 }
181}
182
mistachkinfda06be2011-08-02 00:57:34 +0000183# Copy file $from into $to. This is used because some versions of
184# TCL for windows (notably the 8.4.1 binary package shipped with the
185# current mingw release) have a broken "file copy" command.
186#
187proc copy_file {from to} {
188 do_copy_file false $from $to
189}
190
191proc forcecopy {from to} {
192 do_copy_file true $from $to
193}
194
195proc do_copy_file {force from to} {
196 set nRetry [getFileRetries] ;# Maximum number of retries.
197 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
198
199 # On windows, sometimes even a [file copy -force] can fail. The cause is
200 # usually "tag-alongs" - programs like anti-virus software, automatic backup
201 # tools and various explorer extensions that keep a file open a little longer
202 # than we expect, causing the delete to fail.
203 #
204 # The solution is to wait a short amount of time before retrying the copy.
205 #
206 if {$nRetry > 0} {
207 for {set i 0} {$i<$nRetry} {incr i} {
208 set rc [catch {
209 if {$force} {
210 file copy -force $from $to
211 } else {
212 file copy $from $to
213 }
214 } msg]
215 if {$rc==0} break
216 if {$nDelay > 0} { after $nDelay }
217 }
218 if {$rc} { error $msg }
219 } else {
220 if {$force} {
221 file copy -force $from $to
222 } else {
223 file copy $from $to
224 }
225 }
226}
227
mistachkinc5484652012-03-05 22:52:33 +0000228# Check if a file name is relative
229#
230proc is_relative_file { file } {
231 return [expr {[file pathtype $file] != "absolute"}]
232}
233
mistachkin4a41f342012-03-06 03:00:49 +0000234# If the VFS supports using the current directory, returns [pwd];
235# otherwise, it returns only the provided suffix string (which is
236# empty by default).
237#
238proc test_pwd { args } {
239 if {[llength $args] > 0} {
240 set suffix1 [lindex $args 0]
241 if {[llength $args] > 1} {
242 set suffix2 [lindex $args 1]
243 } else {
244 set suffix2 $suffix1
245 }
246 } else {
247 set suffix1 ""; set suffix2 ""
248 }
249 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000250 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000251 } else {
252 return $suffix2
253 }
254}
255
mistachkinfda06be2011-08-02 00:57:34 +0000256# Delete a file or directory
257#
258proc delete_file {args} {
259 do_delete_file false {*}$args
260}
261
262proc forcedelete {args} {
263 do_delete_file true {*}$args
264}
265
266proc do_delete_file {force args} {
267 set nRetry [getFileRetries] ;# Maximum number of retries.
268 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
269
270 foreach filename $args {
271 # On windows, sometimes even a [file delete -force] can fail just after
272 # a file is closed. The cause is usually "tag-alongs" - programs like
273 # anti-virus software, automatic backup tools and various explorer
274 # extensions that keep a file open a little longer than we expect, causing
275 # the delete to fail.
276 #
277 # The solution is to wait a short amount of time before retrying the
278 # delete.
279 #
280 if {$nRetry > 0} {
281 for {set i 0} {$i<$nRetry} {incr i} {
282 set rc [catch {
283 if {$force} {
284 file delete -force $filename
285 } else {
286 file delete $filename
287 }
288 } msg]
289 if {$rc==0} break
290 if {$nDelay > 0} { after $nDelay }
291 }
292 if {$rc} { error $msg }
293 } else {
294 if {$force} {
295 file delete -force $filename
296 } else {
297 file delete $filename
298 }
299 }
300 }
301}
302
mistachkin1d406e02013-08-29 01:09:14 +0000303if {$::tcl_platform(platform) eq "windows"} {
304 proc do_remove_win32_dir {args} {
305 set nRetry [getFileRetries] ;# Maximum number of retries.
306 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
307
308 foreach dirName $args {
309 # On windows, sometimes even a [remove_win32_dir] can fail just after
310 # a directory is emptied. The cause is usually "tag-alongs" - programs
311 # like anti-virus software, automatic backup tools and various explorer
312 # extensions that keep a file open a little longer than we expect,
313 # causing the delete to fail.
314 #
315 # The solution is to wait a short amount of time before retrying the
316 # removal.
317 #
318 if {$nRetry > 0} {
319 for {set i 0} {$i < $nRetry} {incr i} {
320 set rc [catch {
321 remove_win32_dir $dirName
322 } msg]
323 if {$rc == 0} break
324 if {$nDelay > 0} { after $nDelay }
325 }
326 if {$rc} { error $msg }
327 } else {
328 remove_win32_dir $dirName
329 }
330 }
331 }
332
333 proc do_delete_win32_file {args} {
334 set nRetry [getFileRetries] ;# Maximum number of retries.
335 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
336
337 foreach fileName $args {
338 # On windows, sometimes even a [delete_win32_file] can fail just after
339 # a file is closed. The cause is usually "tag-alongs" - programs like
340 # anti-virus software, automatic backup tools and various explorer
341 # extensions that keep a file open a little longer than we expect,
342 # causing the delete to fail.
343 #
344 # The solution is to wait a short amount of time before retrying the
345 # delete.
346 #
347 if {$nRetry > 0} {
348 for {set i 0} {$i < $nRetry} {incr i} {
349 set rc [catch {
350 delete_win32_file $fileName
351 } msg]
352 if {$rc == 0} break
353 if {$nDelay > 0} { after $nDelay }
354 }
355 if {$rc} { error $msg }
356 } else {
357 delete_win32_file $fileName
358 }
359 }
360 }
361}
362
dancb354602010-07-08 09:44:42 +0000363proc execpresql {handle args} {
364 trace remove execution $handle enter [list execpresql $handle]
365 if {[info exists ::G(perm:presql)]} {
366 $handle eval $::G(perm:presql)
367 }
368}
369
dan68928b62010-06-22 13:46:43 +0000370# This command should be called after loading tester.tcl from within
371# all test scripts that are incompatible with encryption codecs.
372#
373proc do_not_use_codec {} {
374 set ::do_not_use_codec 1
375 reset_db
376}
drh422dded2016-07-25 16:10:43 +0000377unset -nocomplain do_not_use_codec
dan68928b62010-06-22 13:46:43 +0000378
drhaf3906a2016-03-14 17:05:04 +0000379# Return true if the "reserved_bytes" integer on database files is non-zero.
380#
381proc nonzero_reserved_bytes {} {
382 return [sqlite3 -has-codec]
383}
384
drh4b7b1c92016-02-15 19:38:17 +0000385# Print a HELP message and exit
386#
387proc print_help_and_quit {} {
388 puts {Options:
389 --pause Wait for user input before continuing
390 --soft-heap-limit=N Set the soft-heap-limit to N
drh31999c52019-11-14 17:46:32 +0000391 --hard-heap-limit=N Set the hard-heap-limit to N
drh4b7b1c92016-02-15 19:38:17 +0000392 --maxerror=N Quit after N errors
393 --verbose=(0|1) Control the amount of output. Default '1'
394 --output=FILE set --verbose=2 and output to FILE. Implies -q
395 -q Shorthand for --verbose=0
396 --help This message
397}
398 exit 1
399}
400
dan430e74c2010-06-07 17:47:26 +0000401# The following block only runs the first time this file is sourced. It
402# does not run in slave interpreters (since the ::cmdlinearg array is
403# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000404#
danc1a60c52010-06-07 14:28:16 +0000405if {[info exists cmdlinearg]==0} {
406
mistachkin1d406e02013-08-29 01:09:14 +0000407 # Parse any options specified in the $argv array. This script accepts the
408 # following options:
danc1a60c52010-06-07 14:28:16 +0000409 #
410 # --pause
411 # --soft-heap-limit=NN
drh31999c52019-11-14 17:46:32 +0000412 # --hard-heap-limit=NN
danc1a60c52010-06-07 14:28:16 +0000413 # --maxerror=NN
414 # --malloctrace=N
415 # --backtrace=N
416 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000417 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000418 # --file-retries=N
419 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000420 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000421 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000422 # --verbose=$val
423 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000424 # -q Reduce output
425 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000426 # --help
danc1a60c52010-06-07 14:28:16 +0000427 #
428 set cmdlinearg(soft-heap-limit) 0
drh31999c52019-11-14 17:46:32 +0000429 set cmdlinearg(hard-heap-limit) 0
danc1a60c52010-06-07 14:28:16 +0000430 set cmdlinearg(maxerror) 1000
431 set cmdlinearg(malloctrace) 0
432 set cmdlinearg(backtrace) 10
433 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000434 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000435 set cmdlinearg(file-retries) 0
436 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000437 set cmdlinearg(start) ""
438 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000439 set cmdlinearg(verbose) ""
440 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000441 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000442
443 set leftover [list]
444 foreach a $argv {
445 switch -regexp -- $a {
446 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000447 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000448 # opportunity to connect profiling tools to the process.
449 puts -nonewline "Press RETURN to begin..."
450 flush stdout
451 gets stdin
452 }
453 {^-+soft-heap-limit=.+$} {
454 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
455 }
drh31999c52019-11-14 17:46:32 +0000456 {^-+hard-heap-limit=.+$} {
457 foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break
458 }
danc1a60c52010-06-07 14:28:16 +0000459 {^-+maxerror=.+$} {
460 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
461 }
462 {^-+malloctrace=.+$} {
463 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
464 if {$cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +0000465 if {0==$::sqlite_options(memdebug)} {
466 set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build"
467 puts stderr $err
468 exit 1
469 }
danc1a60c52010-06-07 14:28:16 +0000470 sqlite3_memdebug_log start
471 }
472 }
473 {^-+backtrace=.+$} {
474 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan2a86c192017-01-25 17:44:13 +0000475 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danc1a60c52010-06-07 14:28:16 +0000476 }
danc1a60c52010-06-07 14:28:16 +0000477 {^-+binarylog=.+$} {
478 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000479 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000480 }
dan0626dfc2010-06-15 06:56:37 +0000481 {^-+soak=.+$} {
482 foreach {dummy cmdlinearg(soak)} [split $a =] break
483 set ::G(issoak) $cmdlinearg(soak)
484 }
mistachkinfda06be2011-08-02 00:57:34 +0000485 {^-+file-retries=.+$} {
486 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
487 set ::G(file-retries) $cmdlinearg(file-retries)
488 }
489 {^-+file-retry-delay=.+$} {
490 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
491 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
492 }
dan6a64d672011-04-04 15:38:16 +0000493 {^-+start=.+$} {
494 foreach {dummy cmdlinearg(start)} [split $a =] break
495
496 set ::G(start:file) $cmdlinearg(start)
497 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
498 set ::G(start:permutation) ${s.perm}
499 set ::G(start:file) ${s.file}
500 }
501 if {$::G(start:file) == ""} {unset ::G(start:file)}
502 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000503 {^-+match=.+$} {
504 foreach {dummy cmdlinearg(match)} [split $a =] break
505
506 set ::G(match) $cmdlinearg(match)
507 if {$::G(match) == ""} {unset ::G(match)}
508 }
dan17c08232015-06-09 15:58:28 +0000509
510 {^-+output=.+$} {
511 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000512 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000513 if {$cmdlinearg(verbose)==""} {
514 set cmdlinearg(verbose) 2
515 }
516 }
517 {^-+verbose=.+$} {
518 foreach {dummy cmdlinearg(verbose)} [split $a =] break
519 if {$cmdlinearg(verbose)=="file"} {
520 set cmdlinearg(verbose) 2
521 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
522 error "option --verbose= must be set to a boolean or to \"file\""
523 }
524 }
drhd5704a82016-03-14 13:42:29 +0000525 {^-+testdir=.*$} {
526 foreach {dummy cmdlinearg(testdir)} [split $a =] break
527 }
drh4b7b1c92016-02-15 19:38:17 +0000528 {.*help.*} {
529 print_help_and_quit
530 }
531 {^-q$} {
532 set cmdlinearg(output) test-out.txt
533 set cmdlinearg(verbose) 2
534 }
dan17c08232015-06-09 15:58:28 +0000535
danc1a60c52010-06-07 14:28:16 +0000536 default {
dan40cf36f2016-04-30 19:23:10 +0000537 if {[file tail $a]==$a} {
538 lappend leftover $a
539 } else {
540 lappend leftover [file normalize $a]
541 }
danc1a60c52010-06-07 14:28:16 +0000542 }
543 }
544 }
drhbea14132016-03-14 14:28:43 +0000545 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000546 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000547 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000548 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000549 if {$cmdlinearg(testdir)!=""} {
550 file mkdir $cmdlinearg(testdir)
551 cd $cmdlinearg(testdir)
552 }
danc1a60c52010-06-07 14:28:16 +0000553 set argv $leftover
554
dan430e74c2010-06-07 17:47:26 +0000555 # Install the malloc layer used to inject OOM errors. And the 'automatic'
556 # extensions. This only needs to be done once for the process.
557 #
mistachkin1d406e02013-08-29 01:09:14 +0000558 sqlite3_shutdown
559 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000560 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000561 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000562
dan430e74c2010-06-07 17:47:26 +0000563 # If the --binarylog option was specified, create the logging VFS. This
564 # call installs the new VFS as the default for all SQLite connections.
565 #
danc1a60c52010-06-07 14:28:16 +0000566 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000567 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000568 }
569
570 # Set the backtrace depth, if malloc tracing is enabled.
571 #
572 if {$cmdlinearg(malloctrace)} {
573 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000574 }
dan17c08232015-06-09 15:58:28 +0000575
576 if {$cmdlinearg(output)!=""} {
577 puts "Copying output to file $cmdlinearg(output)"
578 set ::G(output_fd) [open $cmdlinearg(output) w]
579 fconfigure $::G(output_fd) -buffering line
580 }
581
582 if {$cmdlinearg(verbose)==""} {
583 set cmdlinearg(verbose) 1
584 }
dan1d07f1d2019-04-01 17:24:20 +0000585
586 if {[info commands vdbe_coverage]!=""} {
587 vdbe_coverage start
588 }
danielk1977f7b9d662008-06-23 18:49:43 +0000589}
danielk197703ba3fa2009-01-09 10:49:14 +0000590
danc1a60c52010-06-07 14:28:16 +0000591# Update the soft-heap-limit each time this script is run. In that
592# way if an individual test file changes the soft-heap-limit, it
593# will be reset at the start of the next test file.
594#
drh31999c52019-11-14 17:46:32 +0000595sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit)
596sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit)
danc1a60c52010-06-07 14:28:16 +0000597
598# Create a test database
599#
danielk197703ba3fa2009-01-09 10:49:14 +0000600proc reset_db {} {
601 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000602 forcedelete test.db
603 forcedelete test.db-journal
604 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000605 sqlite3 db ./test.db
606 set ::DB [sqlite3_connection_pointer db]
607 if {[info exists ::SETUP_SQL]} {
608 db eval $::SETUP_SQL
609 }
drhcd61c282002-03-06 22:01:34 +0000610}
danielk197703ba3fa2009-01-09 10:49:14 +0000611reset_db
drhbec2bf42000-05-29 23:48:22 +0000612
613# Abort early if this script has been run before.
614#
danc1a60c52010-06-07 14:28:16 +0000615if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000616
drhce2198c2010-09-10 13:22:59 +0000617# Make sure memory statistics are enabled.
618#
619sqlite3_config_memstatus 1
620
danc1a60c52010-06-07 14:28:16 +0000621# Initialize the test counters and set up commands to access them.
622# Or, if this is a slave interpreter, set up aliases to write the
623# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000624#
danc1a60c52010-06-07 14:28:16 +0000625if {0==[info exists ::SLAVE]} {
626 set TC(errors) 0
627 set TC(count) 0
628 set TC(fail_list) [list]
629 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000630 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000631
632 proc set_test_counter {counter args} {
633 if {[llength $args]} {
634 set ::TC($counter) [lindex $args 0]
635 }
636 set ::TC($counter)
637 }
drhb62c3352006-11-23 09:39:16 +0000638}
drh348784e2000-05-29 20:41:49 +0000639
drh521cc842008-04-15 02:36:33 +0000640# Record the fact that a sequence of tests were omitted.
641#
mistachkin6c3c1a02011-11-12 03:17:40 +0000642proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000643 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000644 if {$append} {
645 lappend omitList [list $name $reason]
646 }
danc1a60c52010-06-07 14:28:16 +0000647 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000648}
649
danc1a60c52010-06-07 14:28:16 +0000650# Record the fact that a test failed.
651#
652proc fail_test {name} {
653 set f [set_test_counter fail_list]
654 lappend f $name
655 set_test_counter fail_list $f
656 set_test_counter errors [expr [set_test_counter errors] + 1]
657
658 set nFail [set_test_counter errors]
659 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000660 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000661 finalize_testing
662 }
663}
664
drhcca17c32013-04-19 12:32:52 +0000665# Remember a warning message to be displayed at the conclusion of all testing
666#
667proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000668 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000669 set warnList [set_test_counter warn_list]
670 if {$append} {
671 lappend warnList $msg
672 }
673 set_test_counter warn_list $warnList
674}
675
676
danc1a60c52010-06-07 14:28:16 +0000677# Increment the number of tests run
678#
679proc incr_ntest {} {
680 set_test_counter count [expr [set_test_counter count] + 1]
681}
682
dan17c08232015-06-09 15:58:28 +0000683# Return true if --verbose=1 was specified on the command line. Otherwise,
684# return false.
685#
686proc verbose {} {
687 return $::cmdlinearg(verbose)
688}
689
690# Use the following commands instead of [puts] for test output within
691# this file. Test scripts can still use regular [puts], which is directed
692# to stdout and, if one is open, the --output file.
693#
694# output1: output that should be printed if --verbose=1 was specified.
695# output2: output that should be printed unconditionally.
696# output2_if_no_verbose: output that should be printed only if --verbose=0.
697#
698proc output1 {args} {
699 set v [verbose]
700 if {$v==1} {
701 uplevel output2 $args
702 } elseif {$v==2} {
703 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
704 }
705}
706proc output2 {args} {
707 set nArg [llength $args]
708 uplevel puts $args
709}
710proc output2_if_no_verbose {args} {
711 set v [verbose]
712 if {$v==0} {
713 uplevel output2 $args
714 } elseif {$v==2} {
715 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
716 }
717}
718
719# Override the [puts] command so that if no channel is explicitly
720# specified the string is written to both stdout and to the file
721# specified by "--output=", if any.
722#
723proc puts_override {args} {
724 set nArg [llength $args]
725 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
726 uplevel puts_original $args
727 if {[info exists ::G(output_fd)]} {
728 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
729 }
730 } else {
731 # A channel was explicitly specified.
732 uplevel puts_original $args
733 }
734}
735rename puts puts_original
736proc puts {args} { uplevel puts_override $args }
737
danc1a60c52010-06-07 14:28:16 +0000738
mistachkin1d406e02013-08-29 01:09:14 +0000739# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000740#
drhe39cd912016-07-09 17:47:01 +0000741# The $expected parameter is the expected result. The result is the return
742# value from the last TCL command in $cmd.
743#
744# Normally, $expected must match exactly. But if $expected is of the form
745# "/regexp/" then regular expression matching is used. If $expected is
746# "~/regexp/" then the regular expression must NOT match. If $expected is
747# of the form "#/value-list/" then each term in value-list must be numeric
748# and must approximately match the corresponding numeric term in $result.
749# Values must match within 10%. Or if the $expected term is A..B then the
750# $result term must be in between A and B.
751#
drh348784e2000-05-29 20:41:49 +0000752proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000753 global argv cmdlinearg
754
dan8c408002010-11-01 17:38:24 +0000755 fix_testname name
756
drh4a50aac2007-08-23 02:47:53 +0000757 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000758
mistachkin1d406e02013-08-29 01:09:14 +0000759# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000760# set go 1
761# } else {
762# set go 0
763# foreach pattern $argv {
764# if {[string match $pattern $name]} {
765# set go 1
766# break
767# }
768# }
769# }
dan430e74c2010-06-07 17:47:26 +0000770
dand506de02010-07-03 13:59:01 +0000771 if {[info exists ::G(perm:prefix)]} {
772 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000773 }
774
danc1a60c52010-06-07 14:28:16 +0000775 incr_ntest
dan17c08232015-06-09 15:58:28 +0000776 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000777 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000778
779 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
780 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000781 output2_if_no_verbose -nonewline $name...
782 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000783 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000784 } else {
drhe39cd912016-07-09 17:47:01 +0000785 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000786 # "expected" is of the form "/PATTERN/" then the result if correct if
787 # regular expression PATTERN matches the result. "~/PATTERN/" means
788 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000789 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000790 set re [string range $expected 2 end-1]
791 if {[string index $re 0]=="*"} {
792 # If the regular expression begins with * then treat it as a glob instead
793 set ok [string match $re $result]
794 } else {
795 set re [string map {# {[-0-9.]+}} $re]
796 set ok [regexp $re $result]
797 }
798 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000799 } elseif {[string index $expected 0]=="#"} {
800 # Numeric range value comparison. Each term of the $result is matched
801 # against one term of $expect. Both $result and $expected terms must be
802 # numeric. The values must match within 10%. Or if $expected is of the
803 # form A..B then the $result term must be between A and B.
804 set e2 [string range $expected 2 end-1]
805 foreach i $result j $e2 {
806 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
807 set ok [expr {$i+0>=$A && $i+0<=$B}]
808 } else {
809 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
810 }
811 if {!$ok} break
812 }
813 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000814 } else {
drhc2b23e72013-11-13 15:32:15 +0000815 set re [string range $expected 1 end-1]
816 if {[string index $re 0]=="*"} {
817 # If the regular expression begins with * then treat it as a glob instead
818 set ok [string match $re $result]
819 } else {
820 set re [string map {# {[-0-9.]+}} $re]
821 set ok [regexp $re $result]
822 }
drh3f17aef2012-04-27 01:08:02 +0000823 }
drh70bdcc72013-05-30 19:28:34 +0000824 } elseif {[regexp {^~?\*.*\*$} $expected]} {
825 # "expected" is of the form "*GLOB*" then the result if correct if
826 # glob pattern GLOB matches the result. "~/GLOB/" means
827 # the glob must not match.
828 if {[string index $expected 0]=="~"} {
829 set e [string range $expected 1 end]
830 set ok [expr {![string match $e $result]}]
831 } else {
832 set ok [string match $expected $result]
833 }
drh3f17aef2012-04-27 01:08:02 +0000834 } else {
835 set ok [expr {[string compare $result $expected]==0}]
836 }
837 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000838 # if {![info exists ::testprefix] || $::testprefix eq ""} {
839 # error "no test prefix"
840 # }
drhd66b2e02015-11-12 21:42:40 +0000841 output1 ""
drh061d35c2015-11-13 00:03:14 +0000842 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000843 fail_test $name
844 } else {
dan17c08232015-06-09 15:58:28 +0000845 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000846 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000847 }
drh348784e2000-05-29 20:41:49 +0000848 } else {
dan17c08232015-06-09 15:58:28 +0000849 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000850 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000851 }
drh6558db82007-04-13 03:23:21 +0000852 flush stdout
drh348784e2000-05-29 20:41:49 +0000853}
dana3f51082010-09-30 18:43:14 +0000854
mistachkinf21979d2015-01-18 05:35:01 +0000855proc dumpbytes {s} {
856 set r ""
857 for {set i 0} {$i < [string length $s]} {incr i} {
858 if {$i > 0} {append r " "}
859 append r [format %02X [scan [string index $s $i] %c]]
860 }
861 return $r
862}
863
drh8df91852012-04-24 12:46:05 +0000864proc catchcmd {db {cmd ""}} {
865 global CLI
866 set out [open cmds.txt w]
867 puts $out $cmd
868 close $out
869 set line "exec $CLI $db < cmds.txt"
870 set rc [catch { eval $line } msg]
871 list $rc $msg
872}
873
mistachkinf21979d2015-01-18 05:35:01 +0000874proc catchcmdex {db {cmd ""}} {
875 global CLI
876 set out [open cmds.txt w]
877 fconfigure $out -encoding binary -translation binary
878 puts -nonewline $out $cmd
879 close $out
880 set line "exec -keepnewline -- $CLI $db < cmds.txt"
881 set chans [list stdin stdout stderr]
882 foreach chan $chans {
883 catch {
884 set modes($chan) [fconfigure $chan]
885 fconfigure $chan -encoding binary -translation binary -buffering none
886 }
887 }
888 set rc [catch { eval $line } msg]
889 foreach chan $chans {
890 catch {
891 eval fconfigure [list $chan] $modes($chan)
892 }
893 }
894 # puts [dumpbytes $msg]
895 list $rc $msg
896}
897
shaneh55505172011-06-21 19:30:19 +0000898proc filepath_normalize {p} {
899 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000900 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000901 # lreverse*2 as a hack to remove any unneeded {} after the string map
902 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000903 } {
904 set p
shaneh55505172011-06-21 19:30:19 +0000905 }
906}
907proc do_filepath_test {name cmd expected} {
908 uplevel [list do_test $name [
909 subst -nocommands { filepath_normalize [ $cmd ] }
910 ] [filepath_normalize $expected]]
911}
912
dan33f53792011-05-05 19:44:22 +0000913proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000914 # different TCL versions display floating point values differently.
915 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000916}
917proc do_realnum_test {name cmd expected} {
918 uplevel [list do_test $name [
919 subst -nocommands { realnum_normalize [ $cmd ] }
920 ] [realnum_normalize $expected]]
921}
922
dana3f51082010-09-30 18:43:14 +0000923proc fix_testname {varname} {
924 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000925 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000926 && [string is digit [string range $testname 0 0]]
927 } {
928 set testname "${::testprefix}-$testname"
929 }
930}
dan67340072011-04-16 19:23:10 +0000931
932proc normalize_list {L} {
933 set L2 [list]
934 foreach l $L {lappend L2 $l}
935 set L2
936}
drh5f9742e2013-08-29 15:08:38 +0000937
danff677b22017-02-04 17:33:30 +0000938# Either:
939#
940# do_execsql_test TESTNAME SQL ?RES?
941# do_execsql_test -db DB TESTNAME SQL ?RES?
942#
943proc do_execsql_test {args} {
944 set db db
945 if {[lindex $args 0]=="-db"} {
946 set db [lindex $args 1]
947 set args [lrange $args 2 end]
948 }
949
950 if {[llength $args]==2} {
951 foreach {testname sql} $args {}
952 set result ""
953 } elseif {[llength $args]==3} {
954 foreach {testname sql result} $args {}
dan9274ad82019-01-14 19:13:30 +0000955
956 # With some versions of Tcl on windows, if $result is all whitespace but
957 # contains some CR/LF characters, the [list {*}$result] below returns a
958 # copy of $result instead of a zero length string. Not clear exactly why
959 # this is. The following is a workaround.
960 if {[llength $result]==0} { set result "" }
danff677b22017-02-04 17:33:30 +0000961 } else {
962 error [string trim {
963 wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?"
964 }]
965 }
966
dana3f51082010-09-30 18:43:14 +0000967 fix_testname testname
danff677b22017-02-04 17:33:30 +0000968
969 uplevel do_test \
970 [list $testname] \
971 [list "execsql {$sql} $db"] \
972 [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000973}
danff677b22017-02-04 17:33:30 +0000974
dan153eda02010-06-21 07:45:47 +0000975proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000976 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000977 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000978}
drh3c2aeae2014-01-24 14:37:44 +0000979proc do_timed_execsql_test {testname sql {result {}}} {
980 fix_testname testname
981 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
982 [list [list {*}$result]]
983}
drh03c39052018-05-02 14:24:34 +0000984
985# Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output
986# as an ASCII-art graph and return a string that is that graph.
987#
988# Hexadecimal literals in the output text are converted into "xxxxxx" since those
989# literals are pointer values that might very from one run of the test to the
990# next, yet we want the output to be consistent.
991#
992proc query_plan_graph {sql} {
993 db eval "EXPLAIN QUERY PLAN $sql" {
994 set dx($id) $detail
995 lappend cx($parent) $id
996 }
997 set a "\n QUERY PLAN\n"
998 append a [append_graph " " dx cx 0]
drhfef37762018-07-10 19:48:35 +0000999 regsub -all { 0x[A-F0-9]+\y} $a { xxxxxx} a
1000 regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a
1001 return $a
dan39854792010-11-15 16:12:58 +00001002}
dan153eda02010-06-21 07:45:47 +00001003
drh03c39052018-05-02 14:24:34 +00001004# Helper routine for [query_plan_graph SQL]:
1005#
1006# Output rows of the graph that are children of $level.
1007#
1008# prefix: Prepend to every output line
1009#
1010# dxname: Name of an array variable that stores text describe
1011# The description for $id is $dx($id)
1012#
1013# cxname: Name of an array variable holding children of item.
1014# Children of $id are $cx($id)
1015#
1016# level: Render all lines that are children of $level
1017#
1018proc append_graph {prefix dxname cxname level} {
1019 upvar $dxname dx $cxname cx
1020 set a ""
1021 set x $cx($level)
1022 set n [llength $x]
1023 for {set i 0} {$i<$n} {incr i} {
1024 set id [lindex $x $i]
1025 if {$i==$n-1} {
1026 set p1 "`--"
1027 set p2 " "
1028 } else {
1029 set p1 "|--"
1030 set p2 "| "
1031 }
1032 append a $prefix$p1$dx($id)\n
1033 if {[info exists cx($id)]} {
1034 append a [append_graph "$prefix$p2" dx cx $id]
1035 }
1036 }
1037 return $a
1038}
1039
1040# Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res
1041#
1042# If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the
1043# complete graph which must match the output of [query_plan_graph $sql]
1044# exactly.
1045#
1046# If $res does not begin with "\s+QUERY PLAN\n" then take it is a string
1047# that must be found somewhere in the query plan output.
1048#
1049proc do_eqp_test {name sql res} {
1050 if {[regexp {^\s+QUERY PLAN\n} $res]} {
1051 uplevel do_test $name [list [list query_plan_graph $sql]] [list $res]
1052 } else {
1053 if {[string index $res 0]!="/"} {
1054 set res "/*$res*/"
1055 }
1056 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
1057 }
1058}
1059
1060
dan51f06982010-09-18 19:00:12 +00001061#-------------------------------------------------------------------------
1062# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
1063#
1064# Where switches are:
1065#
1066# -errorformat FMTSTRING
1067# -count
danaf1dcab2010-09-20 19:17:53 +00001068# -query SQL
dana16d1062010-09-28 17:37:28 +00001069# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +00001070# -repair TCL
dan51f06982010-09-18 19:00:12 +00001071#
1072proc do_select_tests {prefix args} {
1073
1074 set testlist [lindex $args end]
1075 set switches [lrange $args 0 end-1]
1076
1077 set errfmt ""
1078 set countonly 0
dana16d1062010-09-28 17:37:28 +00001079 set tclquery ""
danaf7626f2010-09-23 18:47:36 +00001080 set repair ""
dan51f06982010-09-18 19:00:12 +00001081
1082 for {set i 0} {$i < [llength $switches]} {incr i} {
1083 set s [lindex $switches $i]
1084 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +00001085 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +00001086 set tclquery [list execsql [lindex $switches [incr i]]]
1087 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
1088 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +00001089 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +00001090 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +00001091 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
1092 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +00001093 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
1094 set countonly 1
1095 } else {
1096 error "unknown switch: $s"
1097 }
1098 }
1099
1100 if {$countonly && $errfmt!=""} {
1101 error "Cannot use -count and -errorformat together"
1102 }
1103 set nTestlist [llength $testlist]
1104 if {$nTestlist%3 || $nTestlist==0 } {
1105 error "SELECT test list contains [llength $testlist] elements"
1106 }
1107
danaf7626f2010-09-23 18:47:36 +00001108 eval $repair
dan51f06982010-09-18 19:00:12 +00001109 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +00001110 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +00001111 execsql $sql
dana16d1062010-09-28 17:37:28 +00001112 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
1113 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +00001114 set nRow 0
1115 db eval $sql {incr nRow}
1116 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
1117 } elseif {$errfmt==""} {
1118 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
1119 } else {
1120 set res [list 1 [string trim [format $errfmt {*}$res]]]
1121 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
1122 }
danaf7626f2010-09-23 18:47:36 +00001123 eval $repair
dan51f06982010-09-18 19:00:12 +00001124 }
danaf7626f2010-09-23 18:47:36 +00001125
dan51f06982010-09-18 19:00:12 +00001126}
1127
danb04757a2010-09-21 16:59:16 +00001128proc delete_all_data {} {
1129 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1130 db eval "DELETE FROM '[string map {' ''} $t]'"
1131 }
1132}
drh348784e2000-05-29 20:41:49 +00001133
mistachkin1d406e02013-08-29 01:09:14 +00001134# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001135# Return the number of microseconds per statement.
1136#
drh3590f152006-11-23 21:09:10 +00001137proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001138 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001139 flush stdout
1140 set speed [time {sqlite3_exec_nr db $sql}]
1141 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001142 if {$tm == 0} {
1143 set rate [format %20s "many"]
1144 } else {
1145 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1146 }
drh3590f152006-11-23 21:09:10 +00001147 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001148 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001149 global total_time
1150 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001151 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001152}
drh45c236d2008-03-22 01:08:00 +00001153proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001154 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001155 flush stdout
1156 set speed [time {eval $script}]
1157 set tm [lindex $speed 0]
1158 if {$tm == 0} {
1159 set rate [format %20s "many"]
1160 } else {
1161 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1162 }
1163 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001164 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001165 global total_time
1166 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001167 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001168}
drhdd924312007-03-31 22:34:16 +00001169proc speed_trial_init {name} {
1170 global total_time
1171 set total_time 0
dana975b592010-10-08 16:09:43 +00001172 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001173 sqlite3 versdb :memory:
1174 set vers [versdb one {SELECT sqlite_source_id()}]
1175 versdb close
dan17c08232015-06-09 15:58:28 +00001176 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001177}
1178proc speed_trial_summary {name} {
1179 global total_time
dan17c08232015-06-09 15:58:28 +00001180 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001181
1182 if { 0 } {
1183 sqlite3 versdb :memory:
1184 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1185 versdb close
dan17c08232015-06-09 15:58:28 +00001186 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001187 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001188 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001189 }
1190 }
drhb62c3352006-11-23 09:39:16 +00001191}
1192
drh348784e2000-05-29 20:41:49 +00001193# Run this routine last
1194#
1195proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001196 catch {db close}
dane8559832014-07-31 17:35:40 +00001197 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001198 catch {db2 close}
1199 catch {db3 close}
1200 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001201}
1202proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001203 global sqlite_open_file_count
1204
1205 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001206
drh6e142f52000-06-08 13:36:40 +00001207 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001208 catch {db2 close}
1209 catch {db3 close}
1210
drh9bc54492007-10-23 14:49:59 +00001211 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001212 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001213 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001214 db close
drh984bfaa2008-03-19 16:08:53 +00001215 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001216
drh31999c52019-11-14 17:46:32 +00001217 sqlite3_soft_heap_limit64 0
1218 sqlite3_hard_heap_limit64 0
danc1a60c52010-06-07 14:28:16 +00001219 set nTest [incr_ntest]
1220 set nErr [set_test_counter errors]
1221
drhef866372013-05-22 20:49:02 +00001222 set nKnown 0
1223 if {[file readable known-problems.txt]} {
1224 set fd [open known-problems.txt]
1225 set content [read $fd]
1226 close $fd
1227 foreach x $content {set known_error($x) 1}
1228 foreach x [set_test_counter fail_list] {
1229 if {[info exists known_error($x)]} {incr nKnown}
1230 }
1231 }
1232 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001233 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001234 out of $nTest tests"
1235 } else {
drh72bf6a32016-01-07 02:06:55 +00001236 set cpuinfo {}
1237 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1238 append cpuinfo " $::tcl_platform(os)"
1239 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1240 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1241 output2 "SQLite [sqlite3 -sourceid]"
1242 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001243 }
1244 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001245 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001246 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001247 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001248 }
dan17c08232015-06-09 15:58:28 +00001249 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001250 }
drhcca17c32013-04-19 12:32:52 +00001251 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001252 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001253 }
danielk1977ee8b7992009-03-26 17:13:06 +00001254 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001255 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001256 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001257 set prec {}
1258 foreach {rec} [lsort $omitList] {
1259 if {$rec==$prec} continue
1260 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001261 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001262 }
1263 }
drh80788d82006-09-02 14:50:23 +00001264 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001265 output2 "******************************************************************"
1266 output2 "N.B.: The version of TCL that you used to build this test harness"
1267 output2 "is defective in that it does not support 64-bit integers. Some or"
1268 output2 "all of the test failures above might be a result from this defect"
1269 output2 "in your TCL build."
1270 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001271 }
danc1a60c52010-06-07 14:28:16 +00001272 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001273 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001274 }
drh94e92032003-02-16 22:21:32 +00001275 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001276 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001277 incr nErr
1278 }
drheafc43b2010-07-26 18:43:40 +00001279 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1280 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001281 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001282 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001283 incr nErr
dan253c6ee2018-09-18 17:00:06 +00001284 ifcapable mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001285 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001286 sqlite3_memdebug_dump ./memleak.txt
1287 }
drhf3a65f72007-08-22 20:18:21 +00001288 } else {
dan17c08232015-06-09 15:58:28 +00001289 output2 "All memory allocations freed - no leaks"
dan253c6ee2018-09-18 17:00:06 +00001290 ifcapable mem5 {
drhd2bb3272007-10-15 19:34:32 +00001291 sqlite3_memdebug_dump ./memusage.txt
1292 }
drhf3a65f72007-08-22 20:18:21 +00001293 }
drhf7141992008-06-19 00:16:08 +00001294 show_memstats
dan17c08232015-06-09 15:58:28 +00001295 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1296 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001297 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001298 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001299 }
danc1a60c52010-06-07 14:28:16 +00001300 if {$::cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +00001301 output2 "Writing mallocs.tcl..."
1302 memdebug_log_sql mallocs.tcl
danielk197735754ac2008-03-21 17:29:37 +00001303 sqlite3_memdebug_log stop
1304 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001305 if {[sqlite3_memory_used]>0} {
dan253c6ee2018-09-18 17:00:06 +00001306 output2 "Writing leaks.tcl..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001307 sqlite3_memdebug_log sync
dan253c6ee2018-09-18 17:00:06 +00001308 memdebug_log_sql leaks.tcl
danielk1977dbdc4d42008-03-28 07:42:53 +00001309 }
danielk197735754ac2008-03-21 17:29:37 +00001310 }
dan1d07f1d2019-04-01 17:24:20 +00001311 if {[info commands vdbe_coverage]!=""} {
1312 vdbe_coverage_report
1313 }
drhd9910fe2006-10-04 11:55:49 +00001314 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001315 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001316 }
1317 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001318 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001319 }
drhdb25e382001-03-15 18:21:22 +00001320 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001321}
1322
dan1d07f1d2019-04-01 17:24:20 +00001323proc vdbe_coverage_report {} {
1324 puts "Writing vdbe coverage report to vdbe_coverage.txt"
1325 set lSrc [list]
1326 set iLine 0
1327 if {[file exists ../sqlite3.c]} {
1328 set fd [open ../sqlite3.c]
1329 set iLine
1330 while { ![eof $fd] } {
1331 set line [gets $fd]
1332 incr iLine
1333 if {[regexp {^/\** Begin file (.*\.c) \**/} $line -> file]} {
1334 lappend lSrc [list $iLine $file]
1335 }
1336 }
1337 close $fd
1338 }
1339 set fd [open vdbe_coverage.txt w]
1340 foreach miss [vdbe_coverage report] {
danafb3f3c2019-04-01 18:43:09 +00001341 foreach {line branch never} $miss {}
dan1d07f1d2019-04-01 17:24:20 +00001342 set nextfile ""
1343 while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} {
1344 set nextfile [lindex $lSrc 0 1]
1345 set lSrc [lrange $lSrc 1 end]
1346 }
1347 if {$nextfile != ""} {
1348 puts $fd ""
1349 puts $fd "### $nextfile ###"
1350 }
danafb3f3c2019-04-01 18:43:09 +00001351 puts $fd "Vdbe branch $line: never $never (path $branch)"
dan1d07f1d2019-04-01 17:24:20 +00001352 }
1353 close $fd
1354}
1355
drhf7141992008-06-19 00:16:08 +00001356# Display memory statistics for analysis and debugging purposes.
1357#
1358proc show_memstats {} {
1359 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001360 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1361 set val [format {now %10d max %10d max-size %10d} \
1362 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001363 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001364 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1365 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001366 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001367 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001368 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1369 set val [format {now %10d max %10d max-size %10d} \
1370 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001371 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001372 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1373 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001374 output1 "Page-cache overflow: $val"
drhec424a52008-07-25 15:39:03 +00001375 ifcapable yytrackmaxstackdepth {
1376 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1377 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001378 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001379 }
drhf7141992008-06-19 00:16:08 +00001380}
1381
drh348784e2000-05-29 20:41:49 +00001382# A procedure to execute SQL
1383#
drhc4a3c772001-04-04 11:48:57 +00001384proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001385 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001386 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001387}
drh3c2aeae2014-01-24 14:37:44 +00001388proc execsql_timed {sql {db db}} {
1389 set tm [time {
1390 set x [uplevel [list $db eval $sql]]
1391 } 1]
1392 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001393 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001394 set x
1395}
drh3aadb2e2000-05-31 17:59:25 +00001396
drhadbca9c2001-09-27 15:11:53 +00001397# Execute SQL and catch exceptions.
1398#
1399proc catchsql {sql {db db}} {
1400 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001401 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001402 lappend r $msg
1403 return $r
1404}
1405
drh04096482001-11-09 22:41:44 +00001406# Do an VDBE code dump on the SQL given
1407#
1408proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001409 output2 ""
1410 output2 "addr opcode p1 p2 p3 p4 p5 #"
1411 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001412 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001413 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001414 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1415 ]
drh04096482001-11-09 22:41:44 +00001416 }
1417}
1418
dane83267d2013-11-05 14:19:22 +00001419proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001420 output2 ""
1421 output2 "addr opcode p1 p2 p3 p4 p5 #"
1422 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001423
dane83267d2013-11-05 14:19:22 +00001424
dan5c663c32013-11-06 14:52:40 +00001425 # Set up colors for the different opcodes. Scheme is as follows:
1426 #
1427 # Red: Opcodes that write to a b-tree.
1428 # Blue: Opcodes that reposition or seek a cursor.
1429 # Green: The ResultRow opcode.
1430 #
dandea63f22014-03-03 16:48:47 +00001431 if { [catch {fconfigure stdout -mode}]==0 } {
1432 set R "\033\[31;1m" ;# Red fg
1433 set G "\033\[32;1m" ;# Green fg
1434 set B "\033\[34;1m" ;# Red fg
1435 set D "\033\[39;0m" ;# Default fg
1436 } else {
1437 set R ""
1438 set G ""
1439 set B ""
1440 set D ""
1441 }
dan5c663c32013-11-06 14:52:40 +00001442 foreach opcode {
dan8da209b2016-07-26 18:06:08 +00001443 Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001444 NoConflict Next Prev VNext VPrev VFilter
dan8da209b2016-07-26 18:06:08 +00001445 SorterSort SorterNext NextIfOpen
dan5c663c32013-11-06 14:52:40 +00001446 } {
1447 set color($opcode) $B
1448 }
1449 foreach opcode {ResultRow} {
1450 set color($opcode) $G
1451 }
1452 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1453 set color($opcode) $R
1454 }
1455
dan427ebba2013-11-05 16:39:31 +00001456 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001457 $db eval "explain $sql" {} {
1458 set x($addr) 0
1459 set op($addr) $opcode
1460
dan427ebba2013-11-05 16:39:31 +00001461 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1462 set linebreak($p2) 1
1463 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001464 }
1465
dan8da209b2016-07-26 18:06:08 +00001466 if {$opcode=="Once"} {
1467 for {set i $addr} {$i<$p2} {incr i} {
1468 set star($i) $addr
1469 }
1470 }
1471
dane3ab7292013-11-12 12:30:09 +00001472 if {$opcode=="Next" || $opcode=="Prev"
1473 || $opcode=="VNext" || $opcode=="VPrev"
dan8da209b2016-07-26 18:06:08 +00001474 || $opcode=="SorterNext" || $opcode=="NextIfOpen"
dane3ab7292013-11-12 12:30:09 +00001475 } {
dane83267d2013-11-05 14:19:22 +00001476 for {set i $p2} {$i<$addr} {incr i} {
1477 incr x($i) 2
1478 }
1479 }
1480
1481 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1482 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1483 incr x($i) 2
1484 }
1485 }
dan427ebba2013-11-05 16:39:31 +00001486
1487 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1488 set linebreak([expr $addr+1]) 1
1489 }
dane83267d2013-11-05 14:19:22 +00001490 }
1491
1492 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001493 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001494 output2 ""
dane83267d2013-11-05 14:19:22 +00001495 }
1496 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001497
dan8da209b2016-07-26 18:06:08 +00001498 if {[info exists star($addr)]} {
1499 set ii [expr $x($star($addr))]
1500 append I " "
1501 set I [string replace $I $ii $ii *]
1502 }
1503
dan5c663c32013-11-06 14:52:40 +00001504 set col ""
1505 catch { set col $color($opcode) }
1506
mistachkineeb31ff2015-06-10 23:02:38 +00001507 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001508 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001509 ]
dane83267d2013-11-05 14:19:22 +00001510 }
mistachkineeb31ff2015-06-10 23:02:38 +00001511 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001512}
1513
drhdafc0ce2008-04-17 19:14:02 +00001514# Show the VDBE program for an SQL statement but omit the Trace
1515# opcode at the beginning. This procedure can be used to prove
1516# that different SQL statements generate exactly the same VDBE code.
1517#
1518proc explain_no_trace {sql} {
1519 set tr [db eval "EXPLAIN $sql"]
1520 return [lrange $tr 7 end]
1521}
1522
drh3aadb2e2000-05-31 17:59:25 +00001523# Another procedure to execute SQL. This one includes the field
1524# names in the returned list.
1525#
1526proc execsql2 {sql} {
1527 set result {}
1528 db eval $sql data {
1529 foreach f $data(*) {
1530 lappend result $f $data($f)
1531 }
1532 }
1533 return $result
1534}
drh17a68932001-01-31 13:28:08 +00001535
mistachkin1d406e02013-08-29 01:09:14 +00001536# Use a temporary in-memory database to execute SQL statements
1537#
1538proc memdbsql {sql} {
1539 sqlite3 memdb :memory:
1540 set result [memdb eval $sql]
1541 memdb close
1542 return $result
1543}
1544
drhdde85d92003-03-01 19:45:34 +00001545# Use the non-callback API to execute multiple SQL statements
1546#
1547proc stepsql {dbptr sql} {
1548 set sql [string trim $sql]
1549 set r 0
1550 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001551 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001552 return [list 1 $vm]
1553 }
1554 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001555# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1556# foreach v $VAL {lappend r $v}
1557# }
1558 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1559 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1560 lappend r [sqlite3_column_text $vm $i]
1561 }
drhdde85d92003-03-01 19:45:34 +00001562 }
danielk1977106bb232004-05-21 10:08:53 +00001563 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001564 return [list 1 $errmsg]
1565 }
1566 }
1567 return $r
1568}
1569
drh21504322002-06-25 13:16:02 +00001570# Do an integrity check of the entire database
1571#
danielk197704103022009-02-03 16:51:24 +00001572proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001573 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001574 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001575 }
1576}
1577
drh433dccf2013-02-09 15:37:11 +00001578# Check the extended error code
1579#
1580proc verify_ex_errcode {name expected {db db}} {
1581 do_test $name [list sqlite3_extended_errcode $db] $expected
1582}
1583
danc6055c72011-04-28 18:46:46 +00001584
1585# Return true if the SQL statement passed as the second argument uses a
1586# statement transaction.
1587#
1588proc sql_uses_stmt {db sql} {
1589 set stmt [sqlite3_prepare $db $sql -1 dummy]
1590 set uses [uses_stmt_journal $stmt]
1591 sqlite3_finalize $stmt
1592 return $uses
1593}
1594
danielk1977db4e8862008-01-16 08:24:46 +00001595proc fix_ifcapable_expr {expr} {
1596 set ret ""
1597 set state 0
1598 for {set i 0} {$i < [string length $expr]} {incr i} {
1599 set char [string range $expr $i $i]
1600 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1601 if {$newstate && !$state} {
1602 append ret {$::sqlite_options(}
1603 }
1604 if {!$newstate && $state} {
1605 append ret )
1606 }
1607 append ret $char
1608 set state $newstate
1609 }
1610 if {$state} {append ret )}
1611 return $ret
1612}
1613
mistachkinc60941f2012-09-13 01:51:02 +00001614# Returns non-zero if the capabilities are present; zero otherwise.
1615#
1616proc capable {expr} {
1617 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1618}
1619
drh27d258a2004-10-30 20:23:09 +00001620# Evaluate a boolean expression of capabilities. If true, execute the
1621# code. Omit the code if false.
1622#
danielk19773e8c37e2005-01-21 03:12:14 +00001623proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001624 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1625 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001626 if ($e2) {
1627 set c [catch {uplevel 1 $code} r]
1628 } else {
1629 set c [catch {uplevel 1 $elsecode} r]
1630 }
1631 return -code $c $r
drh21504322002-06-25 13:16:02 +00001632}
danielk197745901d62004-11-10 15:27:38 +00001633
danielk1977aca790a2005-01-13 11:07:52 +00001634# This proc execs a seperate process that crashes midway through executing
1635# the SQL script $sql on database test.db.
1636#
1637# The crash occurs during a sync() of file $crashfile. When the crash
1638# occurs a random subset of all unsynced writes made by the process are
1639# written into the files on disk. Argument $crashdelay indicates the
1640# number of file syncs to wait before crashing.
1641#
1642# The return value is a list of two elements. The first element is a
1643# boolean, indicating whether or not the process actually crashed or
1644# reported some other error. The second element in the returned list is the
1645# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001646# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001647#
danielk1977f8940ae2007-08-23 11:07:10 +00001648# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001649#
1650proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001651
1652 set blocksize ""
1653 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001654 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001655 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001656 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001657 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001658 set dc ""
dancb1b0a62017-03-02 14:51:47 +00001659 set dfltvfs 0
danielk197759a33f92007-03-17 10:26:59 +00001660 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001661
danielk197759a33f92007-03-17 10:26:59 +00001662 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1663 set z [lindex $args $ii]
1664 set n [string length $z]
1665 set z2 [lindex $args [expr $ii+1]]
1666
1667 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001668 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001669 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001670 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001671 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001672 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
dancb1b0a62017-03-02 14:51:47 +00001673 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }\
1674 elseif {$n>1 && [string first $z -dfltvfs]==0} {set dfltvfs $z2 }\
danielk197759a33f92007-03-17 10:26:59 +00001675 else { error "Unrecognized option: $z" }
1676 }
1677
1678 if {$crashfile eq ""} {
1679 error "Compulsory option -file missing"
1680 }
1681
mistachkin1d406e02013-08-29 01:09:14 +00001682 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001683 # cfSync(), which can be different then what TCL uses by
1684 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001685 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001686
1687 set f [open crash.tcl w]
dancb1b0a62017-03-02 14:51:47 +00001688 puts $f "sqlite3_crash_enable 1 $dfltvfs"
danielk1977f8940ae2007-08-23 11:07:10 +00001689 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001690 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001691
1692 # This block sets the cache size of the main database to 10
1693 # pages. This is done in case the build is configured to omit
1694 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001695 if {$opendb!=""} {
1696 puts $f $opendb
1697 puts $f {db eval {SELECT * FROM sqlite_master;}}
1698 puts $f {set bt [btree_from_db db]}
1699 puts $f {btree_set_cache_size $bt 10}
1700 }
dan999cd082013-12-09 20:42:03 +00001701
drhcb1f0f62008-01-08 15:18:52 +00001702 if {$prngseed} {
1703 set seed [expr {$prngseed%10007+1}]
1704 # puts seed=$seed
1705 puts $f "db eval {SELECT randomblob($seed)}"
1706 }
danielk1977933bbd62007-03-17 07:22:42 +00001707
drhf8587402008-01-08 16:03:49 +00001708 if {[string length $tclbody]>0} {
1709 puts $f $tclbody
1710 }
1711 if {[string length $sql]>0} {
1712 puts $f "db eval {"
1713 puts $f "$sql"
1714 puts $f "}"
1715 }
danielk1977aca790a2005-01-13 11:07:52 +00001716 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001717 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001718 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001719 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001720
shanehf2c08822010-07-08 16:30:44 +00001721 # Windows/ActiveState TCL returns a slightly different
1722 # error message. We map that to the expected message
1723 # so that we don't have to change all of the test
1724 # cases.
1725 if {$::tcl_platform(platform)=="windows"} {
1726 if {$msg=="child killed: unknown signal"} {
1727 set msg "child process exited abnormally"
1728 }
1729 }
mistachkin1d406e02013-08-29 01:09:14 +00001730
danielk1977aca790a2005-01-13 11:07:52 +00001731 lappend r $msg
1732}
1733
dan33447e72017-07-22 20:12:31 +00001734# crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL
1735#
1736proc crash_on_write {args} {
1737
1738 set nArg [llength $args]
1739 if {$nArg<2 || $nArg%2} {
1740 error "bad args: $args"
1741 }
1742 set zSql [lindex $args end]
1743 set nDelay [lindex $args end-1]
1744
1745 set devchar {}
1746 for {set ii 0} {$ii < $nArg-2} {incr ii 2} {
1747 set opt [lindex $args $ii]
1748 switch -- [lindex $args $ii] {
1749 -devchar {
1750 set devchar [lindex $args [expr $ii+1]]
1751 }
1752
1753 default { error "unrecognized option: $opt" }
1754 }
1755 }
1756
1757 set f [open crash.tcl w]
1758 puts $f "sqlite3_crash_on_write $nDelay"
1759 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
1760 puts $f "sqlite3 db test.db -vfs writecrash"
1761 puts $f "db eval {$zSql}"
1762 puts $f "set {} {}"
1763
1764 close $f
1765 set r [catch {
1766 exec [info nameofexec] crash.tcl >@stdout
1767 } msg]
1768
1769 # Windows/ActiveState TCL returns a slightly different
1770 # error message. We map that to the expected message
1771 # so that we don't have to change all of the test
1772 # cases.
1773 if {$::tcl_platform(platform)=="windows"} {
1774 if {$msg=="child killed: unknown signal"} {
1775 set msg "child process exited abnormally"
1776 }
1777 }
1778
1779 lappend r $msg
1780}
1781
danb88e24f2013-02-23 18:58:11 +00001782proc run_ioerr_prep {} {
1783 set ::sqlite_io_error_pending 0
1784 catch {db close}
1785 catch {db2 close}
1786 catch {forcedelete test.db}
1787 catch {forcedelete test.db-journal}
1788 catch {forcedelete test2.db}
1789 catch {forcedelete test2.db-journal}
1790 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1791 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1792 if {[info exists ::ioerropts(-tclprep)]} {
1793 eval $::ioerropts(-tclprep)
1794 }
1795 if {[info exists ::ioerropts(-sqlprep)]} {
1796 execsql $::ioerropts(-sqlprep)
1797 }
1798 expr 0
1799}
1800
danielk197732554c12005-01-22 03:39:39 +00001801# Usage: do_ioerr_test <test number> <options...>
1802#
1803# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001804# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001805# used to name the tests executed by this proc. Options are as follows:
1806#
1807# -tclprep TCL script to run to prepare test.
1808# -sqlprep SQL script to run to prepare test.
1809# -tclbody TCL script to run with IO error simulation.
1810# -sqlbody TCL script to run with IO error simulation.
1811# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001812# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001813# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001814# -start Value of 'N' to begin with (default 1)
1815#
1816# -cksum Boolean. If true, test that the database does
1817# not change during the execution of the test case.
1818#
1819proc do_ioerr_test {testname args} {
1820
danielk197732554c12005-01-22 03:39:39 +00001821 set ::ioerropts(-start) 1
1822 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001823 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001824 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001825 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001826 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001827 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001828 array set ::ioerropts $args
1829
danielk197747cd39c2008-05-12 12:41:15 +00001830 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1831 # a couple of obscure IO errors that do not return them.
1832 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001833
danb88e24f2013-02-23 18:58:11 +00001834 # Create a single TCL script from the TCL and SQL specified
1835 # as the body of the test.
1836 set ::ioerrorbody {}
1837 if {[info exists ::ioerropts(-tclbody)]} {
1838 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1839 }
1840 if {[info exists ::ioerropts(-sqlbody)]} {
1841 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1842 }
1843
1844 save_prng_state
1845 if {$::ioerropts(-cksum)} {
1846 run_ioerr_prep
1847 eval $::ioerrorbody
1848 set ::goodcksum [cksum]
1849 }
danielk197747cd39c2008-05-12 12:41:15 +00001850
danielk197732554c12005-01-22 03:39:39 +00001851 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001852 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001853 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001854 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001855 incr ::ioerropts(-count) -1
1856 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001857
danielk197732554c12005-01-22 03:39:39 +00001858 # Skip this IO error if it was specified with the "-exclude" option.
1859 if {[info exists ::ioerropts(-exclude)]} {
1860 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1861 }
danielk1977861f7452008-06-05 11:39:11 +00001862 if {$::ioerropts(-restoreprng)} {
1863 restore_prng_state
1864 }
danielk197732554c12005-01-22 03:39:39 +00001865
mistachkin1d406e02013-08-29 01:09:14 +00001866 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001867 # SQL (in that order) to prepare for the test case.
1868 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001869 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001870 } {0}
1871
1872 # Read the 'checksum' of the database.
1873 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001874 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001875 }
drh93aed5a2008-01-16 17:46:38 +00001876
danielk197732554c12005-01-22 03:39:39 +00001877 # Set the Nth IO error to fail.
1878 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001879 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001880 set ::sqlite_io_error_pending $n
1881 }] $n
danielk197732554c12005-01-22 03:39:39 +00001882
danb88e24f2013-02-23 18:58:11 +00001883 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001884 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001885 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001886 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001887 set ::sqlite_io_error_hit 0
1888 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001889 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001890 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001891 set rc [sqlite3_errcode $::DB]
1892 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001893 # If we are in extended result code mode, make sure all of the
1894 # IOERRs we get back really do have their extended code values.
1895 # If an extended result code is returned, the sqlite3_errcode
1896 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1897 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001898 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1899 return $rc
1900 }
1901 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001902 # If we are not in extended result code mode, make sure no
1903 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001904 if {[regexp {\+\d} $rc]} {
1905 return $rc
1906 }
1907 }
drh93aed5a2008-01-16 17:46:38 +00001908 # The test repeats as long as $::go is non-zero. $::go starts out
1909 # as 1. When a test runs to completion without hitting an I/O
1910 # error, that means there is no point in continuing with this test
1911 # case so set $::go to zero.
1912 #
1913 if {$::sqlite_io_error_pending>0} {
1914 set ::go 0
1915 set q 0
1916 set ::sqlite_io_error_pending 0
1917 } else {
1918 set q 1
1919 }
1920
drhc9ac5ca2005-11-04 22:03:30 +00001921 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001922 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1923 set r 1
1924 }
danielk1977f2fa8312006-01-24 13:09:33 +00001925 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001926
1927 # One of two things must have happened. either
1928 # 1. We never hit the IO error and the SQL returned OK
1929 # 2. An IO error was hit and the SQL failed
1930 #
dand41a29a2010-05-06 15:56:28 +00001931 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001932 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001933 } {1}
1934
danielk197780daec62008-05-12 10:57:02 +00001935 set ::sqlite_io_error_hit 0
1936 set ::sqlite_io_error_pending 0
1937
mistachkin1d406e02013-08-29 01:09:14 +00001938 # Check that no page references were leaked. There should be
1939 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001940 # or zero otherwise.
1941 #
danielk197781620542008-06-07 05:19:37 +00001942 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001943 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001944 # occurs while attempting to detect a hot-journal file), then
1945 # there may 0 page references and an active transaction according
1946 # to [sqlite3_get_autocommit].
1947 #
danielk19774abd5442008-05-05 15:26:50 +00001948 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001949 do_test $testname.$n.4 {
1950 set bt [btree_from_db db]
1951 db_enter db
1952 array set stats [btree_pager_stats $bt]
1953 db_leave db
danielk197781620542008-06-07 05:19:37 +00001954 set nRef $stats(ref)
1955 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1956 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001957 }
1958
mistachkin1d406e02013-08-29 01:09:14 +00001959 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001960 # and the pager is not running in exclusive-locking mode,
1961 # check that the pager is in "unlocked" state. Theoretically,
1962 # if a call to xUnlock() failed due to an IO error the underlying
1963 # file may still be locked.
1964 #
1965 ifcapable pragma {
1966 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001967 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001968 && [db one {pragma locking_mode}] eq "normal"
1969 && [sqlite3_get_autocommit db]
1970 } {
1971 do_test $testname.$n.5 {
1972 set bt [btree_from_db db]
1973 db_enter db
1974 array set stats [btree_pager_stats $bt]
1975 db_leave db
1976 set stats(state)
1977 } 0
1978 }
1979 }
1980
mistachkin48864df2013-03-21 21:20:32 +00001981 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001982 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001983 #
drh1aa5af12008-03-07 19:51:14 +00001984 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001985 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001986 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001987 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001988 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001989 set nowcksum [cksum]
1990 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1991 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001992 output2 "now=$nowcksum"
1993 output2 "the=$::checksum"
1994 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001995 }
1996 set res
1997 } 1
danielk197732554c12005-01-22 03:39:39 +00001998 }
1999
drh1aa5af12008-03-07 19:51:14 +00002000 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00002001 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00002002 if {[info exists ::ioerropts(-cleanup)]} {
2003 catch $::ioerropts(-cleanup)
2004 }
danielk197732554c12005-01-22 03:39:39 +00002005 }
2006 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00002007 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00002008 unset ::ioerropts
2009}
2010
drh93aed5a2008-01-16 17:46:38 +00002011# Return a checksum based on the contents of the main database associated
2012# with connection $db
danielk197732554c12005-01-22 03:39:39 +00002013#
2014proc cksum {{db db}} {
2015 set txt [$db eval {
2016 SELECT name, type, sql FROM sqlite_master order by name
2017 }]\n
2018 foreach tbl [$db eval {
2019 SELECT name FROM sqlite_master WHERE type='table' order by name
2020 }] {
2021 append txt [$db eval "SELECT * FROM $tbl"]\n
2022 }
2023 foreach prag {default_synchronous default_cache_size} {
2024 append txt $prag-[$db eval "PRAGMA $prag"]\n
2025 }
2026 set cksum [string length $txt]-[md5 $txt]
2027 # puts $cksum-[file size test.db]
2028 return $cksum
2029}
2030
drh93aed5a2008-01-16 17:46:38 +00002031# Generate a checksum based on the contents of the main and temp tables
2032# database $db. If the checksum of two databases is the same, and the
2033# integrity-check passes for both, the two databases are identical.
2034#
drh1db639c2008-01-17 02:36:28 +00002035proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00002036 set ret [list]
2037 ifcapable tempdb {
2038 set sql {
2039 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2040 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
2041 SELECT 'sqlite_master' UNION
2042 SELECT 'sqlite_temp_master' ORDER BY 1
2043 }
2044 } else {
2045 set sql {
2046 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2047 SELECT 'sqlite_master' ORDER BY 1
2048 }
2049 }
2050 set tbllist [$db eval $sql]
2051 set txt {}
2052 foreach tbl $tbllist {
2053 append txt [$db eval "SELECT * FROM $tbl"]
2054 }
2055 foreach prag {default_cache_size} {
2056 append txt $prag-[$db eval "PRAGMA $prag"]\n
2057 }
2058 # puts txt=$txt
2059 return [md5 $txt]
2060}
2061
drhdc2c4912009-02-04 22:46:47 +00002062# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00002063# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00002064# Examples of $dbname are "temp" or "main".
2065#
2066proc dbcksum {db dbname} {
2067 if {$dbname=="temp"} {
2068 set master sqlite_temp_master
2069 } else {
2070 set master $dbname.sqlite_master
2071 }
2072 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
2073 set txt [$db eval "SELECT * FROM $master"]\n
2074 foreach tab $alltab {
2075 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
2076 }
2077 return [md5 $txt]
2078}
2079
dan253c6ee2018-09-18 17:00:06 +00002080proc memdebug_log_sql {filename} {
danielk197735754ac2008-03-21 17:29:37 +00002081
danielk19776f332c12008-03-21 14:22:44 +00002082 set data [sqlite3_memdebug_log dump]
2083 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00002084 if {$nFrame < 0} { return "" }
2085
danielk197735754ac2008-03-21 17:29:37 +00002086 set database temp
2087
danielk19776ab3a2e2009-02-19 14:39:25 +00002088 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00002089
2090 set sql ""
2091 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00002092 set nCall [lindex $e 0]
2093 set nByte [lindex $e 1]
2094 set lStack [lrange $e 2 end]
2095 append sql "INSERT INTO ${database}.malloc VALUES"
2096 append sql "('test', $nCall, $nByte, '$lStack');\n"
2097 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00002098 set frames($f) 1
2099 }
2100 }
2101
2102 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00002103 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00002104
dandee9be92019-02-05 16:53:26 +00002105 set pid [pid]
2106
danielk19776f332c12008-03-21 14:22:44 +00002107 foreach f [array names frames] {
2108 set addr [format %x $f]
dandee9be92019-02-05 16:53:26 +00002109 set cmd "eu-addr2line --pid=$pid $addr"
danielk19776f332c12008-03-21 14:22:44 +00002110 set line [eval exec $cmd]
2111 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00002112
2113 set file [lindex [split $line :] 0]
2114 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00002115 }
2116
danielk197735754ac2008-03-21 17:29:37 +00002117 foreach f [array names files] {
2118 set contents ""
2119 catch {
2120 set fd [open $f]
2121 set contents [read $fd]
2122 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002123 }
danielk197735754ac2008-03-21 17:29:37 +00002124 set contents [string map {' ''} $contents]
2125 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00002126 }
danielk19776f332c12008-03-21 14:22:44 +00002127
dan253c6ee2018-09-18 17:00:06 +00002128 set escaped "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
2129 set escaped [string map [list "{" "\\{" "}" "\\}"] $escaped]
2130
danielk197735754ac2008-03-21 17:29:37 +00002131 set fd [open $filename w]
dan253c6ee2018-09-18 17:00:06 +00002132 puts $fd "set BUILTIN {"
2133 puts $fd $escaped
2134 puts $fd "}"
2135 puts $fd {set BUILTIN [string map [list "\\{" "{" "\\}" "}"] $BUILTIN]}
2136 set mtv [open $::testdir/malloctraceviewer.tcl]
2137 set txt [read $mtv]
2138 close $mtv
2139 puts $fd $txt
danielk197735754ac2008-03-21 17:29:37 +00002140 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002141}
drh93aed5a2008-01-16 17:46:38 +00002142
danf5894502009-10-07 18:41:19 +00002143# Drop all tables in database [db]
2144proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00002145 ifcapable trigger&&foreignkey {
2146 set pk [$db one "PRAGMA foreign_keys"]
2147 $db eval "PRAGMA foreign_keys = OFF"
2148 }
drh9a6ffc82010-02-15 18:03:20 +00002149 foreach {idx name file} [db eval {PRAGMA database_list}] {
2150 if {$idx==1} {
2151 set master sqlite_temp_master
2152 } else {
2153 set master $name.sqlite_master
2154 }
2155 foreach {t type} [$db eval "
2156 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00002157 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00002158 "] {
dana16d1062010-09-28 17:37:28 +00002159 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00002160 }
danf5894502009-10-07 18:41:19 +00002161 }
shaneh4e7b32f2009-12-17 22:12:51 +00002162 ifcapable trigger&&foreignkey {
2163 $db eval "PRAGMA foreign_keys = $pk"
2164 }
danf5894502009-10-07 18:41:19 +00002165}
2166
dand05a7142016-08-02 17:07:51 +00002167# Drop all auxiliary indexes from the main database opened by handle [db].
2168#
2169proc drop_all_indexes {{db db}} {
2170 set L [$db eval {
2171 SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%'
2172 }]
2173 foreach idx $L { $db eval "DROP INDEX $idx" }
2174}
2175
2176
dan71cb5182010-04-26 12:39:03 +00002177#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00002178# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00002179# "wal", then the tests are run in WAL mode. Otherwise, they should be run
2180# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00002181# intrusive:
dan71cb5182010-04-26 12:39:03 +00002182#
2183# wal_set_journal_mode ?DB?
2184#
2185# If running a WAL test, execute "PRAGMA journal_mode = wal" using
2186# connection handle DB. Otherwise, this command is a no-op.
2187#
2188# wal_check_journal_mode TESTNAME ?DB?
2189#
2190# If running a WAL test, execute a tests case that fails if the main
2191# database for connection handle DB is not currently a WAL database.
2192# Otherwise (if not running a WAL permutation) this is a no-op.
2193#
2194# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00002195#
dan71cb5182010-04-26 12:39:03 +00002196# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00002197#
dan71cb5182010-04-26 12:39:03 +00002198proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00002199 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00002200}
2201proc wal_set_journal_mode {{db db}} {
2202 if { [wal_is_wal_mode] } {
2203 $db eval "PRAGMA journal_mode = WAL"
2204 }
2205}
2206proc wal_check_journal_mode {testname {db db}} {
2207 if { [wal_is_wal_mode] } {
2208 $db eval { SELECT * FROM sqlite_master }
2209 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
2210 }
2211}
2212
dan05accd22016-04-27 18:54:49 +00002213proc wal_is_capable {} {
2214 ifcapable !wal { return 0 }
2215 if {[permutation]=="journaltest"} { return 0 }
2216 return 1
2217}
2218
dan430e74c2010-06-07 17:47:26 +00002219proc permutation {} {
2220 set perm ""
2221 catch {set perm $::G(perm:name)}
2222 set perm
2223}
dancb79e512010-08-06 13:50:07 +00002224proc presql {} {
2225 set presql ""
2226 catch {set presql $::G(perm:presql)}
2227 set presql
2228}
dan430e74c2010-06-07 17:47:26 +00002229
danbe7721d2016-02-04 17:31:03 +00002230proc isquick {} {
2231 set ret 0
2232 catch {set ret $::G(isquick)}
2233 set ret
2234}
2235
danc1a60c52010-06-07 14:28:16 +00002236#-------------------------------------------------------------------------
2237#
2238proc slave_test_script {script} {
2239
2240 # Create the interpreter used to run the test script.
2241 interp create tinterp
2242
2243 # Populate some global variables that tester.tcl expects to see.
2244 foreach {var value} [list \
2245 ::argv0 $::argv0 \
2246 ::argv {} \
2247 ::SLAVE 1 \
2248 ] {
2249 interp eval tinterp [list set $var $value]
2250 }
2251
dan17c08232015-06-09 15:58:28 +00002252 # If output is being copied into a file, share the file-descriptor with
2253 # the interpreter.
2254 if {[info exists ::G(output_fd)]} {
2255 interp share {} $::G(output_fd) tinterp
2256 }
2257
danc1a60c52010-06-07 14:28:16 +00002258 # The alias used to access the global test counters.
2259 tinterp alias set_test_counter set_test_counter
2260
2261 # Set up the ::cmdlinearg array in the slave.
2262 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2263
dan430e74c2010-06-07 17:47:26 +00002264 # Set up the ::G array in the slave.
2265 interp eval tinterp [list array set ::G [array get ::G]]
2266
danc1a60c52010-06-07 14:28:16 +00002267 # Load the various test interfaces implemented in C.
2268 load_testfixture_extensions tinterp
2269
2270 # Run the test script.
2271 interp eval tinterp $script
2272
danea5542d2010-07-06 11:26:15 +00002273 # Check if the interpreter call [run_thread_tests]
2274 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2275 set ::run_thread_tests_called 1
2276 }
2277
danc1a60c52010-06-07 14:28:16 +00002278 # Delete the interpreter used to run the test script.
2279 interp delete tinterp
2280}
2281
dan430e74c2010-06-07 17:47:26 +00002282proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002283 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002284
dan6a64d672011-04-04 15:38:16 +00002285 if {[info exists ::G(start:permutation)]} {
2286 if {[permutation] != $::G(start:permutation)} return
2287 unset ::G(start:permutation)
2288 }
2289 if {[info exists ::G(start:file)]} {
2290 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2291 unset ::G(start:file)
2292 }
2293
dan92d516a2010-07-05 14:54:48 +00002294 # Remember the value of the shared-cache setting. So that it is possible
2295 # to check afterwards that it was not modified by the test script.
2296 #
2297 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002298
dan92d516a2010-07-05 14:54:48 +00002299 # Run the test script in a slave interpreter.
2300 #
danea5542d2010-07-06 11:26:15 +00002301 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002302 reset_prng_state
2303 set ::sqlite_open_file_count 0
2304 set time [time { slave_test_script [list source $zFile] }]
2305 set ms [expr [lindex $time 0] / 1000]
2306
dan92d516a2010-07-05 14:54:48 +00002307 # Test that all files opened by the test script were closed. Omit this
2308 # if the test script has "thread" in its name. The open file counter
2309 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002310 #
danea5542d2010-07-06 11:26:15 +00002311 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002312 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2313 }
dan430e74c2010-06-07 17:47:26 +00002314 set ::sqlite_open_file_count 0
2315
mistachkin1d406e02013-08-29 01:09:14 +00002316 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002317 # the test script.
2318 #
mistachkin1d406e02013-08-29 01:09:14 +00002319 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002320 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2321 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2322 }
dan430e74c2010-06-07 17:47:26 +00002323
dan92d516a2010-07-05 14:54:48 +00002324 # Add some info to the output.
2325 #
dan17c08232015-06-09 15:58:28 +00002326 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002327 show_memstats
danc1a60c52010-06-07 14:28:16 +00002328}
2329
dan59257dc2010-08-04 11:34:31 +00002330# Open a new connection on database test.db and execute the SQL script
2331# supplied as an argument. Before returning, close the new conection and
2332# restore the 4 byte fields starting at header offsets 28, 92 and 96
2333# to the values they held before the SQL was executed. This simulates
2334# a write by a pre-3.7.0 client.
2335#
2336proc sql36231 {sql} {
2337 set B [hexio_read test.db 92 8]
2338 set A [hexio_read test.db 28 4]
2339 sqlite3 db36231 test.db
2340 catch { db36231 func a_string a_string }
2341 execsql $sql db36231
2342 db36231 close
2343 hexio_write test.db 28 $A
2344 hexio_write test.db 92 $B
2345 return ""
2346}
danf5894502009-10-07 18:41:19 +00002347
danccc7ff42010-11-29 12:06:45 +00002348proc db_save {} {
2349 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2350 foreach f [glob -nocomplain test.db*] {
2351 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002352 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002353 }
2354}
2355proc db_save_and_close {} {
2356 db_save
2357 catch { db close }
2358 return ""
2359}
2360proc db_restore {} {
2361 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2362 foreach f2 [glob -nocomplain sv_test.db*] {
2363 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002364 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002365 }
2366}
2367proc db_restore_and_reopen {{dbfile test.db}} {
2368 catch { db close }
2369 db_restore
2370 sqlite3 db $dbfile
2371}
2372proc db_delete_and_reopen {{file test.db}} {
2373 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002374 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002375 sqlite3 db $file
2376}
2377
dan03bc5252015-07-24 14:17:17 +00002378# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2379# to configure the size of the PAGECACHE allocation using the parameters
2380# provided to this command. Save the old PAGECACHE parameters in a global
2381# variable so that [test_restore_config_pagecache] can restore the previous
2382# configuration.
2383#
2384# Before returning, reopen connection [db] on file test.db.
2385#
2386proc test_set_config_pagecache {sz nPg} {
2387 catch {db close}
2388 catch {db2 close}
2389 catch {db3 close}
2390
2391 sqlite3_shutdown
2392 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2393 sqlite3_initialize
2394 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002395 reset_db
dan03bc5252015-07-24 14:17:17 +00002396}
2397
2398# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2399# to configure the size of the PAGECACHE allocation to the size saved in
2400# the global variable by an earlier call to [test_set_config_pagecache].
2401#
2402# Before returning, reopen connection [db] on file test.db.
2403#
2404proc test_restore_config_pagecache {} {
2405 catch {db close}
2406 catch {db2 close}
2407 catch {db3 close}
2408
2409 sqlite3_shutdown
2410 eval sqlite3_config_pagecache $::old_pagecache_config
2411 unset ::old_pagecache_config
2412 sqlite3_initialize
2413 autoinstall_test_functions
2414 sqlite3 db test.db
2415}
2416
dan43efc182017-12-19 17:42:13 +00002417proc test_binary_name {nm} {
dan089555c2016-03-15 09:55:44 +00002418 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002419 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002420 } else {
dan1e8dae02016-03-19 14:53:36 +00002421 set ret $nm
dan089555c2016-03-15 09:55:44 +00002422 }
dan43efc182017-12-19 17:42:13 +00002423 file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]
2424}
2425
2426proc test_find_binary {nm} {
2427 set ret [test_binary_name $nm]
dan089555c2016-03-15 09:55:44 +00002428 if {![file executable $ret]} {
2429 finish_test
dan49aed582016-03-19 15:13:59 +00002430 return ""
dan089555c2016-03-15 09:55:44 +00002431 }
2432 return $ret
2433}
2434
danielk197745901d62004-11-10 15:27:38 +00002435# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2436# the tests in shell[1-5].test. If no such executable can be found, invoke
2437# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002438#
dan64b95bb2012-05-12 05:30:29 +00002439proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002440 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002441 if {$prog==""} { return -code return }
2442 return $prog
drh348784e2000-05-29 20:41:49 +00002443}
2444
dan1e8dae02016-03-19 14:53:36 +00002445# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2446# the tests in sqldiff tests. If no such executable can be found, invoke
2447# [finish_test ; return] in the callers context.
2448#
2449proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002450 set prog [test_find_binary sqldiff]
2451 if {$prog==""} { return -code return }
2452 return $prog
dan1e8dae02016-03-19 14:53:36 +00002453}
2454
daneab0e102018-02-07 18:02:50 +00002455# Call sqlite3_expanded_sql() on all statements associated with database
2456# connection $db. This sometimes finds use-after-free bugs if run with
2457# valgrind or address-sanitizer.
2458proc expand_all_sql {db} {
2459 set stmt ""
2460 while {[set stmt [sqlite3_next_stmt $db $stmt]]!=""} {
2461 sqlite3_expanded_sql $stmt
2462 }
2463}
2464
dan1e8dae02016-03-19 14:53:36 +00002465
drh348784e2000-05-29 20:41:49 +00002466# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2467# to non-zero, then set the global variable $AUTOVACUUM to 1.
2468set AUTOVACUUM $sqlite_options(default_autovacuum)
2469
dan64b95bb2012-05-12 05:30:29 +00002470# Make sure the FTS enhanced query syntax is disabled.
2471set sqlite_fts3_enable_parentheses 0
2472
drh348784e2000-05-29 20:41:49 +00002473# During testing, assume that all database files are well-formed. The
2474# few test cases that deliberately corrupt database files should rescind
2475# this setting by invoking "database_can_be_corrupt"
2476#
2477database_never_corrupt
2478
drh348784e2000-05-29 20:41:49 +00002479source $testdir/thread_common.tcl
2480source $testdir/malloc_common.tcl