blob: b1acb0686ab2ecdd64d48e7820118f9ce2cd9974 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements some common TCL routines used for regression
12# testing the SQLite library
13#
drhbb77b752009-04-09 01:23:49 +000014# $Id: tester.tcl,v 1.143 2009/04/09 01:23:49 drh Exp $
drhfbc3eab2001-04-06 16:13:42 +000015
danc1a60c52010-06-07 14:28:16 +000016#-------------------------------------------------------------------------
mistachkin1d406e02013-08-29 01:09:14 +000017# The commands provided by the code in this file to help with creating
danc1a60c52010-06-07 14:28:16 +000018# test cases are as follows:
drh8359d8c2008-03-29 11:00:54 +000019#
danc1a60c52010-06-07 14:28:16 +000020# Commands to manipulate the db and the file-system at a high level:
drh8359d8c2008-03-29 11:00:54 +000021#
mistachkinc5484652012-03-05 22:52:33 +000022# is_relative_file
mistachkin4a41f342012-03-06 03:00:49 +000023# test_pwd
mistachkinf8a78462012-03-08 20:00:36 +000024# get_pwd
danc1a60c52010-06-07 14:28:16 +000025# copy_file FROM TO
mistachkinfda06be2011-08-02 00:57:34 +000026# delete_file FILENAME
dan67340072011-04-16 19:23:10 +000027# drop_all_tables ?DB?
dand05a7142016-08-02 17:07:51 +000028# drop_all_indexes ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000029# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000030# forcedelete FILENAME
31#
32# Test the capability of the SQLite version built into the interpreter to
33# determine if a specific test can be run:
34#
mistachkinc60941f2012-09-13 01:51:02 +000035# capable EXPR
danc1a60c52010-06-07 14:28:16 +000036# ifcapable EXPR
37#
38# Calulate checksums based on database contents:
39#
40# dbcksum DB DBNAME
41# allcksum ?DB?
42# cksum ?DB?
43#
44# Commands to execute/explain SQL statements:
45#
mistachkin1d406e02013-08-29 01:09:14 +000046# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000047# stepsql DB SQL
48# execsql2 SQL
49# explain_no_trace SQL
50# explain SQL ?DB?
51# catchsql SQL ?DB?
52# execsql SQL ?DB?
53#
54# Commands to run test cases:
55#
56# do_ioerr_test TESTNAME ARGS...
57# crashsql ARGS...
58# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000059# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000060# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000061# do_execsql_test TESTNAME SQL EXPECTED
62# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000063# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000064#
dan430e74c2010-06-07 17:47:26 +000065# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000066#
67# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000068# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000069# fail_test TESTNAME
70# incr_ntest
71#
72# Command run at the end of each test file:
73#
74# finish_test
75#
dan430e74c2010-06-07 17:47:26 +000076# Commands to help create test files that run with the "WAL" and other
77# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000078#
79# wal_is_wal_mode
80# wal_set_journal_mode ?DB?
81# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000082# permutation
dancb79e512010-08-06 13:50:07 +000083# presql
danc1a60c52010-06-07 14:28:16 +000084#
dan17c08232015-06-09 15:58:28 +000085# Command to test whether or not --verbose=1 was specified on the command
86# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
87# output file only").
88#
89# verbose
90#
drh348784e2000-05-29 20:41:49 +000091
mistachkin1d406e02013-08-29 01:09:14 +000092# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000093# configure SQLite to take database file locks on the page that begins
94# 64KB into the database file instead of the one 1GB in. This means
95# the code that handles that special case can be tested without creating
96# very large database files.
97#
drh92febd92004-08-20 18:34:20 +000098set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000099sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +0000100
danc1a60c52010-06-07 14:28:16 +0000101
mistachkin1d406e02013-08-29 01:09:14 +0000102# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000103# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000104#
danc1a60c52010-06-07 14:28:16 +0000105# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000106#
danc1a60c52010-06-07 14:28:16 +0000107# becomes
drh4a50aac2007-08-23 02:47:53 +0000108#
danc1a60c52010-06-07 14:28:16 +0000109# sqlite3 db test.db -key {xyzzy}
drh9eb9e262004-02-11 02:18:05 +0000110#
dan430e74c2010-06-07 17:47:26 +0000111if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000112 rename sqlite3 sqlite_orig
113 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000114 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000115 # This command is opening a new database connection.
116 #
117 if {[info exists ::G(perm:sqlite3_args)]} {
118 set args [concat $args $::G(perm:sqlite3_args)]
119 }
dan68928b62010-06-22 13:46:43 +0000120 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000121 lappend args -key {xyzzy}
122 }
123
dan3ccd20a2010-06-09 19:01:02 +0000124 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000125 if {[info exists ::G(perm:presql)]} {
126 [lindex $args 0] eval $::G(perm:presql)
127 }
dan1ce1b4a2010-12-07 14:32:28 +0000128 if {[info exists ::G(perm:dbconfig)]} {
129 set ::dbhandle [lindex $args 0]
130 uplevel #0 $::G(perm:dbconfig)
131 }
dan9a23d262020-07-16 14:52:24 +0000132 [lindex $args 0] cache size 3
dan3ccd20a2010-06-09 19:01:02 +0000133 set res
dan430e74c2010-06-07 17:47:26 +0000134 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000135 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000136 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000137 #
138 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000139 }
drh9eb9e262004-02-11 02:18:05 +0000140 }
141}
142
mistachkinfda06be2011-08-02 00:57:34 +0000143proc getFileRetries {} {
144 if {![info exists ::G(file-retries)]} {
145 #
146 # NOTE: Return the default number of retries for [file] operations. A
147 # value of zero or less here means "disabled".
148 #
mistachkinc60941f2012-09-13 01:51:02 +0000149 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000150 }
151 return $::G(file-retries)
152}
153
154proc getFileRetryDelay {} {
155 if {![info exists ::G(file-retry-delay)]} {
156 #
157 # NOTE: Return the default number of milliseconds to wait when retrying
158 # failed [file] operations. A value of zero or less means "do not
159 # wait".
160 #
161 return 100; # TODO: Good default?
162 }
163 return $::G(file-retry-delay)
164}
165
mistachkinf8a78462012-03-08 20:00:36 +0000166# Return the string representing the name of the current directory. On
167# Windows, the result is "normalized" to whatever our parent command shell
168# is using to prevent case-mismatch issues.
169#
170proc get_pwd {} {
171 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000172 #
173 # NOTE: Cannot use [file normalize] here because it would alter the
174 # case of the result to what Tcl considers canonical, which would
175 # defeat the purpose of this procedure.
176 #
177 return [string map [list \\ /] \
178 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000179 } else {
180 return [pwd]
181 }
182}
183
mistachkinfda06be2011-08-02 00:57:34 +0000184# Copy file $from into $to. This is used because some versions of
185# TCL for windows (notably the 8.4.1 binary package shipped with the
186# current mingw release) have a broken "file copy" command.
187#
188proc copy_file {from to} {
189 do_copy_file false $from $to
190}
191
192proc forcecopy {from to} {
193 do_copy_file true $from $to
194}
195
196proc do_copy_file {force from to} {
197 set nRetry [getFileRetries] ;# Maximum number of retries.
198 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
199
200 # On windows, sometimes even a [file copy -force] can fail. The cause is
201 # usually "tag-alongs" - programs like anti-virus software, automatic backup
202 # tools and various explorer extensions that keep a file open a little longer
203 # than we expect, causing the delete to fail.
204 #
205 # The solution is to wait a short amount of time before retrying the copy.
206 #
207 if {$nRetry > 0} {
208 for {set i 0} {$i<$nRetry} {incr i} {
209 set rc [catch {
210 if {$force} {
211 file copy -force $from $to
212 } else {
213 file copy $from $to
214 }
215 } msg]
216 if {$rc==0} break
217 if {$nDelay > 0} { after $nDelay }
218 }
219 if {$rc} { error $msg }
220 } else {
221 if {$force} {
222 file copy -force $from $to
223 } else {
224 file copy $from $to
225 }
226 }
227}
228
mistachkinc5484652012-03-05 22:52:33 +0000229# Check if a file name is relative
230#
231proc is_relative_file { file } {
232 return [expr {[file pathtype $file] != "absolute"}]
233}
234
mistachkin4a41f342012-03-06 03:00:49 +0000235# If the VFS supports using the current directory, returns [pwd];
236# otherwise, it returns only the provided suffix string (which is
237# empty by default).
238#
239proc test_pwd { args } {
240 if {[llength $args] > 0} {
241 set suffix1 [lindex $args 0]
242 if {[llength $args] > 1} {
243 set suffix2 [lindex $args 1]
244 } else {
245 set suffix2 $suffix1
246 }
247 } else {
248 set suffix1 ""; set suffix2 ""
249 }
250 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000251 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000252 } else {
253 return $suffix2
254 }
255}
256
mistachkinfda06be2011-08-02 00:57:34 +0000257# Delete a file or directory
258#
259proc delete_file {args} {
260 do_delete_file false {*}$args
261}
262
263proc forcedelete {args} {
264 do_delete_file true {*}$args
265}
266
267proc do_delete_file {force args} {
268 set nRetry [getFileRetries] ;# Maximum number of retries.
269 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
270
271 foreach filename $args {
272 # On windows, sometimes even a [file delete -force] can fail just after
273 # a file is closed. The cause is usually "tag-alongs" - programs like
274 # anti-virus software, automatic backup tools and various explorer
275 # extensions that keep a file open a little longer than we expect, causing
276 # the delete to fail.
277 #
278 # The solution is to wait a short amount of time before retrying the
279 # delete.
280 #
281 if {$nRetry > 0} {
282 for {set i 0} {$i<$nRetry} {incr i} {
283 set rc [catch {
284 if {$force} {
285 file delete -force $filename
286 } else {
287 file delete $filename
288 }
289 } msg]
290 if {$rc==0} break
291 if {$nDelay > 0} { after $nDelay }
292 }
293 if {$rc} { error $msg }
294 } else {
295 if {$force} {
296 file delete -force $filename
297 } else {
298 file delete $filename
299 }
300 }
301 }
302}
303
mistachkin1d406e02013-08-29 01:09:14 +0000304if {$::tcl_platform(platform) eq "windows"} {
305 proc do_remove_win32_dir {args} {
306 set nRetry [getFileRetries] ;# Maximum number of retries.
307 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
308
309 foreach dirName $args {
310 # On windows, sometimes even a [remove_win32_dir] can fail just after
311 # a directory is emptied. The cause is usually "tag-alongs" - programs
312 # like anti-virus software, automatic backup tools and various explorer
313 # extensions that keep a file open a little longer than we expect,
314 # causing the delete to fail.
315 #
316 # The solution is to wait a short amount of time before retrying the
317 # removal.
318 #
319 if {$nRetry > 0} {
320 for {set i 0} {$i < $nRetry} {incr i} {
321 set rc [catch {
322 remove_win32_dir $dirName
323 } msg]
324 if {$rc == 0} break
325 if {$nDelay > 0} { after $nDelay }
326 }
327 if {$rc} { error $msg }
328 } else {
329 remove_win32_dir $dirName
330 }
331 }
332 }
333
334 proc do_delete_win32_file {args} {
335 set nRetry [getFileRetries] ;# Maximum number of retries.
336 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
337
338 foreach fileName $args {
339 # On windows, sometimes even a [delete_win32_file] can fail just after
340 # a file is closed. The cause is usually "tag-alongs" - programs like
341 # anti-virus software, automatic backup tools and various explorer
342 # extensions that keep a file open a little longer than we expect,
343 # causing the delete to fail.
344 #
345 # The solution is to wait a short amount of time before retrying the
346 # delete.
347 #
348 if {$nRetry > 0} {
349 for {set i 0} {$i < $nRetry} {incr i} {
350 set rc [catch {
351 delete_win32_file $fileName
352 } msg]
353 if {$rc == 0} break
354 if {$nDelay > 0} { after $nDelay }
355 }
356 if {$rc} { error $msg }
357 } else {
358 delete_win32_file $fileName
359 }
360 }
361 }
362}
363
dancb354602010-07-08 09:44:42 +0000364proc execpresql {handle args} {
365 trace remove execution $handle enter [list execpresql $handle]
366 if {[info exists ::G(perm:presql)]} {
367 $handle eval $::G(perm:presql)
368 }
369}
370
dan68928b62010-06-22 13:46:43 +0000371# This command should be called after loading tester.tcl from within
372# all test scripts that are incompatible with encryption codecs.
373#
374proc do_not_use_codec {} {
375 set ::do_not_use_codec 1
376 reset_db
377}
drh422dded2016-07-25 16:10:43 +0000378unset -nocomplain do_not_use_codec
dan68928b62010-06-22 13:46:43 +0000379
drhaf3906a2016-03-14 17:05:04 +0000380# Return true if the "reserved_bytes" integer on database files is non-zero.
381#
382proc nonzero_reserved_bytes {} {
383 return [sqlite3 -has-codec]
384}
385
drh4b7b1c92016-02-15 19:38:17 +0000386# Print a HELP message and exit
387#
388proc print_help_and_quit {} {
389 puts {Options:
390 --pause Wait for user input before continuing
391 --soft-heap-limit=N Set the soft-heap-limit to N
drh31999c52019-11-14 17:46:32 +0000392 --hard-heap-limit=N Set the hard-heap-limit to N
drh4b7b1c92016-02-15 19:38:17 +0000393 --maxerror=N Quit after N errors
394 --verbose=(0|1) Control the amount of output. Default '1'
395 --output=FILE set --verbose=2 and output to FILE. Implies -q
396 -q Shorthand for --verbose=0
397 --help This message
398}
399 exit 1
400}
401
dan430e74c2010-06-07 17:47:26 +0000402# The following block only runs the first time this file is sourced. It
403# does not run in slave interpreters (since the ::cmdlinearg array is
404# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000405#
danc1a60c52010-06-07 14:28:16 +0000406if {[info exists cmdlinearg]==0} {
407
mistachkin1d406e02013-08-29 01:09:14 +0000408 # Parse any options specified in the $argv array. This script accepts the
409 # following options:
danc1a60c52010-06-07 14:28:16 +0000410 #
411 # --pause
412 # --soft-heap-limit=NN
drh31999c52019-11-14 17:46:32 +0000413 # --hard-heap-limit=NN
danc1a60c52010-06-07 14:28:16 +0000414 # --maxerror=NN
415 # --malloctrace=N
416 # --backtrace=N
417 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000418 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000419 # --file-retries=N
420 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000421 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000422 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000423 # --verbose=$val
424 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000425 # -q Reduce output
426 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000427 # --help
danc1a60c52010-06-07 14:28:16 +0000428 #
429 set cmdlinearg(soft-heap-limit) 0
drh31999c52019-11-14 17:46:32 +0000430 set cmdlinearg(hard-heap-limit) 0
danc1a60c52010-06-07 14:28:16 +0000431 set cmdlinearg(maxerror) 1000
432 set cmdlinearg(malloctrace) 0
433 set cmdlinearg(backtrace) 10
434 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000435 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000436 set cmdlinearg(file-retries) 0
437 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000438 set cmdlinearg(start) ""
439 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000440 set cmdlinearg(verbose) ""
441 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000442 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000443
444 set leftover [list]
445 foreach a $argv {
446 switch -regexp -- $a {
447 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000448 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000449 # opportunity to connect profiling tools to the process.
450 puts -nonewline "Press RETURN to begin..."
451 flush stdout
452 gets stdin
453 }
454 {^-+soft-heap-limit=.+$} {
455 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
456 }
drh31999c52019-11-14 17:46:32 +0000457 {^-+hard-heap-limit=.+$} {
458 foreach {dummy cmdlinearg(hard-heap-limit)} [split $a =] break
459 }
danc1a60c52010-06-07 14:28:16 +0000460 {^-+maxerror=.+$} {
461 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
462 }
463 {^-+malloctrace=.+$} {
464 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
465 if {$cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +0000466 if {0==$::sqlite_options(memdebug)} {
467 set err "Error: --malloctrace=1 requires an SQLITE_MEMDEBUG build"
468 puts stderr $err
469 exit 1
470 }
danc1a60c52010-06-07 14:28:16 +0000471 sqlite3_memdebug_log start
472 }
473 }
474 {^-+backtrace=.+$} {
475 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan2a86c192017-01-25 17:44:13 +0000476 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danc1a60c52010-06-07 14:28:16 +0000477 }
danc1a60c52010-06-07 14:28:16 +0000478 {^-+binarylog=.+$} {
479 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000480 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000481 }
dan0626dfc2010-06-15 06:56:37 +0000482 {^-+soak=.+$} {
483 foreach {dummy cmdlinearg(soak)} [split $a =] break
484 set ::G(issoak) $cmdlinearg(soak)
485 }
mistachkinfda06be2011-08-02 00:57:34 +0000486 {^-+file-retries=.+$} {
487 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
488 set ::G(file-retries) $cmdlinearg(file-retries)
489 }
490 {^-+file-retry-delay=.+$} {
491 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
492 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
493 }
dan6a64d672011-04-04 15:38:16 +0000494 {^-+start=.+$} {
495 foreach {dummy cmdlinearg(start)} [split $a =] break
496
497 set ::G(start:file) $cmdlinearg(start)
498 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
499 set ::G(start:permutation) ${s.perm}
500 set ::G(start:file) ${s.file}
501 }
502 if {$::G(start:file) == ""} {unset ::G(start:file)}
503 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000504 {^-+match=.+$} {
505 foreach {dummy cmdlinearg(match)} [split $a =] break
506
507 set ::G(match) $cmdlinearg(match)
508 if {$::G(match) == ""} {unset ::G(match)}
509 }
dan17c08232015-06-09 15:58:28 +0000510
511 {^-+output=.+$} {
512 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000513 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000514 if {$cmdlinearg(verbose)==""} {
515 set cmdlinearg(verbose) 2
516 }
517 }
518 {^-+verbose=.+$} {
519 foreach {dummy cmdlinearg(verbose)} [split $a =] break
520 if {$cmdlinearg(verbose)=="file"} {
521 set cmdlinearg(verbose) 2
522 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
523 error "option --verbose= must be set to a boolean or to \"file\""
524 }
525 }
drhd5704a82016-03-14 13:42:29 +0000526 {^-+testdir=.*$} {
527 foreach {dummy cmdlinearg(testdir)} [split $a =] break
528 }
drh4b7b1c92016-02-15 19:38:17 +0000529 {.*help.*} {
530 print_help_and_quit
531 }
532 {^-q$} {
533 set cmdlinearg(output) test-out.txt
534 set cmdlinearg(verbose) 2
535 }
dan17c08232015-06-09 15:58:28 +0000536
danc1a60c52010-06-07 14:28:16 +0000537 default {
dan40cf36f2016-04-30 19:23:10 +0000538 if {[file tail $a]==$a} {
539 lappend leftover $a
540 } else {
541 lappend leftover [file normalize $a]
542 }
danc1a60c52010-06-07 14:28:16 +0000543 }
544 }
545 }
drhbea14132016-03-14 14:28:43 +0000546 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000547 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000548 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000549 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000550 if {$cmdlinearg(testdir)!=""} {
551 file mkdir $cmdlinearg(testdir)
552 cd $cmdlinearg(testdir)
553 }
danc1a60c52010-06-07 14:28:16 +0000554 set argv $leftover
555
dan430e74c2010-06-07 17:47:26 +0000556 # Install the malloc layer used to inject OOM errors. And the 'automatic'
557 # extensions. This only needs to be done once for the process.
558 #
mistachkin1d406e02013-08-29 01:09:14 +0000559 sqlite3_shutdown
560 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000561 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000562 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000563
dan430e74c2010-06-07 17:47:26 +0000564 # If the --binarylog option was specified, create the logging VFS. This
565 # call installs the new VFS as the default for all SQLite connections.
566 #
danc1a60c52010-06-07 14:28:16 +0000567 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000568 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000569 }
570
571 # Set the backtrace depth, if malloc tracing is enabled.
572 #
573 if {$cmdlinearg(malloctrace)} {
574 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000575 }
dan17c08232015-06-09 15:58:28 +0000576
577 if {$cmdlinearg(output)!=""} {
578 puts "Copying output to file $cmdlinearg(output)"
579 set ::G(output_fd) [open $cmdlinearg(output) w]
580 fconfigure $::G(output_fd) -buffering line
581 }
582
583 if {$cmdlinearg(verbose)==""} {
584 set cmdlinearg(verbose) 1
585 }
dan1d07f1d2019-04-01 17:24:20 +0000586
587 if {[info commands vdbe_coverage]!=""} {
588 vdbe_coverage start
589 }
danielk1977f7b9d662008-06-23 18:49:43 +0000590}
danielk197703ba3fa2009-01-09 10:49:14 +0000591
danc1a60c52010-06-07 14:28:16 +0000592# Update the soft-heap-limit each time this script is run. In that
593# way if an individual test file changes the soft-heap-limit, it
594# will be reset at the start of the next test file.
595#
drh31999c52019-11-14 17:46:32 +0000596sqlite3_soft_heap_limit64 $cmdlinearg(soft-heap-limit)
597sqlite3_hard_heap_limit64 $cmdlinearg(hard-heap-limit)
danc1a60c52010-06-07 14:28:16 +0000598
599# Create a test database
600#
danielk197703ba3fa2009-01-09 10:49:14 +0000601proc reset_db {} {
602 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000603 forcedelete test.db
604 forcedelete test.db-journal
605 forcedelete test.db-wal
danielk197703ba3fa2009-01-09 10:49:14 +0000606 sqlite3 db ./test.db
607 set ::DB [sqlite3_connection_pointer db]
608 if {[info exists ::SETUP_SQL]} {
609 db eval $::SETUP_SQL
610 }
drhcd61c282002-03-06 22:01:34 +0000611}
danielk197703ba3fa2009-01-09 10:49:14 +0000612reset_db
drhbec2bf42000-05-29 23:48:22 +0000613
614# Abort early if this script has been run before.
615#
danc1a60c52010-06-07 14:28:16 +0000616if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000617
drhce2198c2010-09-10 13:22:59 +0000618# Make sure memory statistics are enabled.
619#
620sqlite3_config_memstatus 1
621
danc1a60c52010-06-07 14:28:16 +0000622# Initialize the test counters and set up commands to access them.
623# Or, if this is a slave interpreter, set up aliases to write the
624# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000625#
danc1a60c52010-06-07 14:28:16 +0000626if {0==[info exists ::SLAVE]} {
627 set TC(errors) 0
628 set TC(count) 0
629 set TC(fail_list) [list]
630 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000631 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000632
633 proc set_test_counter {counter args} {
634 if {[llength $args]} {
635 set ::TC($counter) [lindex $args 0]
636 }
637 set ::TC($counter)
638 }
drhb62c3352006-11-23 09:39:16 +0000639}
drh348784e2000-05-29 20:41:49 +0000640
drh521cc842008-04-15 02:36:33 +0000641# Record the fact that a sequence of tests were omitted.
642#
mistachkin6c3c1a02011-11-12 03:17:40 +0000643proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000644 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000645 if {$append} {
646 lappend omitList [list $name $reason]
647 }
danc1a60c52010-06-07 14:28:16 +0000648 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000649}
650
danc1a60c52010-06-07 14:28:16 +0000651# Record the fact that a test failed.
652#
653proc fail_test {name} {
654 set f [set_test_counter fail_list]
655 lappend f $name
656 set_test_counter fail_list $f
657 set_test_counter errors [expr [set_test_counter errors] + 1]
658
659 set nFail [set_test_counter errors]
660 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000661 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000662 finalize_testing
663 }
664}
665
drhcca17c32013-04-19 12:32:52 +0000666# Remember a warning message to be displayed at the conclusion of all testing
667#
668proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000669 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000670 set warnList [set_test_counter warn_list]
671 if {$append} {
672 lappend warnList $msg
673 }
674 set_test_counter warn_list $warnList
675}
676
677
danc1a60c52010-06-07 14:28:16 +0000678# Increment the number of tests run
679#
680proc incr_ntest {} {
681 set_test_counter count [expr [set_test_counter count] + 1]
682}
683
dan17c08232015-06-09 15:58:28 +0000684# Return true if --verbose=1 was specified on the command line. Otherwise,
685# return false.
686#
687proc verbose {} {
688 return $::cmdlinearg(verbose)
689}
690
691# Use the following commands instead of [puts] for test output within
692# this file. Test scripts can still use regular [puts], which is directed
693# to stdout and, if one is open, the --output file.
694#
695# output1: output that should be printed if --verbose=1 was specified.
696# output2: output that should be printed unconditionally.
697# output2_if_no_verbose: output that should be printed only if --verbose=0.
698#
699proc output1 {args} {
700 set v [verbose]
701 if {$v==1} {
702 uplevel output2 $args
703 } elseif {$v==2} {
704 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
705 }
706}
707proc output2 {args} {
708 set nArg [llength $args]
709 uplevel puts $args
710}
711proc output2_if_no_verbose {args} {
712 set v [verbose]
713 if {$v==0} {
714 uplevel output2 $args
715 } elseif {$v==2} {
716 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
717 }
718}
719
720# Override the [puts] command so that if no channel is explicitly
721# specified the string is written to both stdout and to the file
722# specified by "--output=", if any.
723#
724proc puts_override {args} {
725 set nArg [llength $args]
726 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
727 uplevel puts_original $args
728 if {[info exists ::G(output_fd)]} {
729 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
730 }
731 } else {
732 # A channel was explicitly specified.
733 uplevel puts_original $args
734 }
735}
736rename puts puts_original
737proc puts {args} { uplevel puts_override $args }
738
danc1a60c52010-06-07 14:28:16 +0000739
mistachkin1d406e02013-08-29 01:09:14 +0000740# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000741#
drhe39cd912016-07-09 17:47:01 +0000742# The $expected parameter is the expected result. The result is the return
743# value from the last TCL command in $cmd.
744#
745# Normally, $expected must match exactly. But if $expected is of the form
746# "/regexp/" then regular expression matching is used. If $expected is
747# "~/regexp/" then the regular expression must NOT match. If $expected is
748# of the form "#/value-list/" then each term in value-list must be numeric
749# and must approximately match the corresponding numeric term in $result.
750# Values must match within 10%. Or if the $expected term is A..B then the
751# $result term must be in between A and B.
752#
drh348784e2000-05-29 20:41:49 +0000753proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000754 global argv cmdlinearg
755
dan8c408002010-11-01 17:38:24 +0000756 fix_testname name
757
drh4a50aac2007-08-23 02:47:53 +0000758 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000759
mistachkin1d406e02013-08-29 01:09:14 +0000760# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000761# set go 1
762# } else {
763# set go 0
764# foreach pattern $argv {
765# if {[string match $pattern $name]} {
766# set go 1
767# break
768# }
769# }
770# }
dan430e74c2010-06-07 17:47:26 +0000771
dand506de02010-07-03 13:59:01 +0000772 if {[info exists ::G(perm:prefix)]} {
773 set name "$::G(perm:prefix)$name"
dan430e74c2010-06-07 17:47:26 +0000774 }
775
danc1a60c52010-06-07 14:28:16 +0000776 incr_ntest
dan17c08232015-06-09 15:58:28 +0000777 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000778 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000779
780 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
781 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000782 output2_if_no_verbose -nonewline $name...
783 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000784 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000785 } else {
dan00bd55e2020-03-20 20:54:28 +0000786 if {[permutation]=="maindbname"} {
787 set result [string map [list [string tolower ICECUBE] main] $result]
788 }
drhe39cd912016-07-09 17:47:01 +0000789 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000790 # "expected" is of the form "/PATTERN/" then the result if correct if
791 # regular expression PATTERN matches the result. "~/PATTERN/" means
792 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000793 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000794 set re [string range $expected 2 end-1]
795 if {[string index $re 0]=="*"} {
796 # If the regular expression begins with * then treat it as a glob instead
797 set ok [string match $re $result]
798 } else {
799 set re [string map {# {[-0-9.]+}} $re]
800 set ok [regexp $re $result]
801 }
802 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000803 } elseif {[string index $expected 0]=="#"} {
804 # Numeric range value comparison. Each term of the $result is matched
805 # against one term of $expect. Both $result and $expected terms must be
806 # numeric. The values must match within 10%. Or if $expected is of the
807 # form A..B then the $result term must be between A and B.
808 set e2 [string range $expected 2 end-1]
809 foreach i $result j $e2 {
810 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
811 set ok [expr {$i+0>=$A && $i+0<=$B}]
812 } else {
813 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
814 }
815 if {!$ok} break
816 }
817 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000818 } else {
drhc2b23e72013-11-13 15:32:15 +0000819 set re [string range $expected 1 end-1]
820 if {[string index $re 0]=="*"} {
821 # If the regular expression begins with * then treat it as a glob instead
822 set ok [string match $re $result]
823 } else {
824 set re [string map {# {[-0-9.]+}} $re]
825 set ok [regexp $re $result]
826 }
drh3f17aef2012-04-27 01:08:02 +0000827 }
drh70bdcc72013-05-30 19:28:34 +0000828 } elseif {[regexp {^~?\*.*\*$} $expected]} {
829 # "expected" is of the form "*GLOB*" then the result if correct if
830 # glob pattern GLOB matches the result. "~/GLOB/" means
831 # the glob must not match.
832 if {[string index $expected 0]=="~"} {
833 set e [string range $expected 1 end]
834 set ok [expr {![string match $e $result]}]
835 } else {
836 set ok [string match $expected $result]
837 }
drh3f17aef2012-04-27 01:08:02 +0000838 } else {
839 set ok [expr {[string compare $result $expected]==0}]
840 }
841 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000842 # if {![info exists ::testprefix] || $::testprefix eq ""} {
843 # error "no test prefix"
844 # }
drhd66b2e02015-11-12 21:42:40 +0000845 output1 ""
drh061d35c2015-11-13 00:03:14 +0000846 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000847 fail_test $name
848 } else {
dan17c08232015-06-09 15:58:28 +0000849 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000850 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000851 }
drh348784e2000-05-29 20:41:49 +0000852 } else {
dan17c08232015-06-09 15:58:28 +0000853 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000854 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000855 }
drh6558db82007-04-13 03:23:21 +0000856 flush stdout
drh348784e2000-05-29 20:41:49 +0000857}
dana3f51082010-09-30 18:43:14 +0000858
mistachkinf21979d2015-01-18 05:35:01 +0000859proc dumpbytes {s} {
860 set r ""
861 for {set i 0} {$i < [string length $s]} {incr i} {
862 if {$i > 0} {append r " "}
863 append r [format %02X [scan [string index $s $i] %c]]
864 }
865 return $r
866}
867
drh8df91852012-04-24 12:46:05 +0000868proc catchcmd {db {cmd ""}} {
869 global CLI
870 set out [open cmds.txt w]
871 puts $out $cmd
872 close $out
873 set line "exec $CLI $db < cmds.txt"
874 set rc [catch { eval $line } msg]
875 list $rc $msg
876}
877
mistachkinf21979d2015-01-18 05:35:01 +0000878proc catchcmdex {db {cmd ""}} {
879 global CLI
880 set out [open cmds.txt w]
881 fconfigure $out -encoding binary -translation binary
882 puts -nonewline $out $cmd
883 close $out
884 set line "exec -keepnewline -- $CLI $db < cmds.txt"
885 set chans [list stdin stdout stderr]
886 foreach chan $chans {
887 catch {
888 set modes($chan) [fconfigure $chan]
889 fconfigure $chan -encoding binary -translation binary -buffering none
890 }
891 }
892 set rc [catch { eval $line } msg]
893 foreach chan $chans {
894 catch {
895 eval fconfigure [list $chan] $modes($chan)
896 }
897 }
898 # puts [dumpbytes $msg]
899 list $rc $msg
900}
901
shaneh55505172011-06-21 19:30:19 +0000902proc filepath_normalize {p} {
903 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000904 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000905 # lreverse*2 as a hack to remove any unneeded {} after the string map
906 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000907 } {
908 set p
shaneh55505172011-06-21 19:30:19 +0000909 }
910}
911proc do_filepath_test {name cmd expected} {
912 uplevel [list do_test $name [
913 subst -nocommands { filepath_normalize [ $cmd ] }
914 ] [filepath_normalize $expected]]
915}
916
dan33f53792011-05-05 19:44:22 +0000917proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000918 # different TCL versions display floating point values differently.
919 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000920}
921proc do_realnum_test {name cmd expected} {
922 uplevel [list do_test $name [
923 subst -nocommands { realnum_normalize [ $cmd ] }
924 ] [realnum_normalize $expected]]
925}
926
dana3f51082010-09-30 18:43:14 +0000927proc fix_testname {varname} {
928 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000929 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000930 && [string is digit [string range $testname 0 0]]
931 } {
932 set testname "${::testprefix}-$testname"
933 }
934}
dan67340072011-04-16 19:23:10 +0000935
936proc normalize_list {L} {
937 set L2 [list]
938 foreach l $L {lappend L2 $l}
939 set L2
940}
drh5f9742e2013-08-29 15:08:38 +0000941
danff677b22017-02-04 17:33:30 +0000942# Either:
943#
944# do_execsql_test TESTNAME SQL ?RES?
945# do_execsql_test -db DB TESTNAME SQL ?RES?
946#
947proc do_execsql_test {args} {
948 set db db
949 if {[lindex $args 0]=="-db"} {
950 set db [lindex $args 1]
951 set args [lrange $args 2 end]
952 }
953
954 if {[llength $args]==2} {
955 foreach {testname sql} $args {}
956 set result ""
957 } elseif {[llength $args]==3} {
958 foreach {testname sql result} $args {}
dan9274ad82019-01-14 19:13:30 +0000959
960 # With some versions of Tcl on windows, if $result is all whitespace but
961 # contains some CR/LF characters, the [list {*}$result] below returns a
962 # copy of $result instead of a zero length string. Not clear exactly why
963 # this is. The following is a workaround.
964 if {[llength $result]==0} { set result "" }
danff677b22017-02-04 17:33:30 +0000965 } else {
966 error [string trim {
967 wrong # args: should be "do_execsql_test ?-db DB? testname sql ?result?"
968 }]
969 }
970
dana3f51082010-09-30 18:43:14 +0000971 fix_testname testname
danff677b22017-02-04 17:33:30 +0000972
973 uplevel do_test \
974 [list $testname] \
975 [list "execsql {$sql} $db"] \
976 [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000977}
danff677b22017-02-04 17:33:30 +0000978
dan153eda02010-06-21 07:45:47 +0000979proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000980 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000981 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000982}
drh3c2aeae2014-01-24 14:37:44 +0000983proc do_timed_execsql_test {testname sql {result {}}} {
984 fix_testname testname
985 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
986 [list [list {*}$result]]
987}
drh03c39052018-05-02 14:24:34 +0000988
989# Run an EXPLAIN QUERY PLAN $sql in database "db". Then rewrite the output
990# as an ASCII-art graph and return a string that is that graph.
991#
992# Hexadecimal literals in the output text are converted into "xxxxxx" since those
993# literals are pointer values that might very from one run of the test to the
994# next, yet we want the output to be consistent.
995#
996proc query_plan_graph {sql} {
997 db eval "EXPLAIN QUERY PLAN $sql" {
998 set dx($id) $detail
999 lappend cx($parent) $id
1000 }
1001 set a "\n QUERY PLAN\n"
1002 append a [append_graph " " dx cx 0]
drhfef37762018-07-10 19:48:35 +00001003 regsub -all { 0x[A-F0-9]+\y} $a { xxxxxx} a
1004 regsub -all {(MATERIALIZE|CO-ROUTINE|SUBQUERY) \d+\y} $a {\1 xxxxxx} a
1005 return $a
dan39854792010-11-15 16:12:58 +00001006}
dan153eda02010-06-21 07:45:47 +00001007
drh03c39052018-05-02 14:24:34 +00001008# Helper routine for [query_plan_graph SQL]:
1009#
1010# Output rows of the graph that are children of $level.
1011#
1012# prefix: Prepend to every output line
1013#
1014# dxname: Name of an array variable that stores text describe
1015# The description for $id is $dx($id)
1016#
1017# cxname: Name of an array variable holding children of item.
1018# Children of $id are $cx($id)
1019#
1020# level: Render all lines that are children of $level
1021#
1022proc append_graph {prefix dxname cxname level} {
1023 upvar $dxname dx $cxname cx
1024 set a ""
1025 set x $cx($level)
1026 set n [llength $x]
1027 for {set i 0} {$i<$n} {incr i} {
1028 set id [lindex $x $i]
1029 if {$i==$n-1} {
1030 set p1 "`--"
1031 set p2 " "
1032 } else {
1033 set p1 "|--"
1034 set p2 "| "
1035 }
1036 append a $prefix$p1$dx($id)\n
1037 if {[info exists cx($id)]} {
1038 append a [append_graph "$prefix$p2" dx cx $id]
1039 }
1040 }
1041 return $a
1042}
1043
1044# Do an EXPLAIN QUERY PLAN test on input $sql with expected results $res
1045#
1046# If $res begins with a "\s+QUERY PLAN\n" then it is assumed to be the
1047# complete graph which must match the output of [query_plan_graph $sql]
1048# exactly.
1049#
1050# If $res does not begin with "\s+QUERY PLAN\n" then take it is a string
1051# that must be found somewhere in the query plan output.
1052#
1053proc do_eqp_test {name sql res} {
1054 if {[regexp {^\s+QUERY PLAN\n} $res]} {
1055 uplevel do_test $name [list [list query_plan_graph $sql]] [list $res]
1056 } else {
1057 if {[string index $res 0]!="/"} {
1058 set res "/*$res*/"
1059 }
1060 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
1061 }
1062}
1063
1064
dan51f06982010-09-18 19:00:12 +00001065#-------------------------------------------------------------------------
1066# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
1067#
1068# Where switches are:
1069#
1070# -errorformat FMTSTRING
1071# -count
danaf1dcab2010-09-20 19:17:53 +00001072# -query SQL
dana16d1062010-09-28 17:37:28 +00001073# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +00001074# -repair TCL
dan51f06982010-09-18 19:00:12 +00001075#
1076proc do_select_tests {prefix args} {
1077
1078 set testlist [lindex $args end]
1079 set switches [lrange $args 0 end-1]
1080
1081 set errfmt ""
1082 set countonly 0
dana16d1062010-09-28 17:37:28 +00001083 set tclquery ""
danaf7626f2010-09-23 18:47:36 +00001084 set repair ""
dan51f06982010-09-18 19:00:12 +00001085
1086 for {set i 0} {$i < [llength $switches]} {incr i} {
1087 set s [lindex $switches $i]
1088 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +00001089 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +00001090 set tclquery [list execsql [lindex $switches [incr i]]]
1091 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
1092 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +00001093 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +00001094 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +00001095 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
1096 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +00001097 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
1098 set countonly 1
1099 } else {
1100 error "unknown switch: $s"
1101 }
1102 }
1103
1104 if {$countonly && $errfmt!=""} {
1105 error "Cannot use -count and -errorformat together"
1106 }
1107 set nTestlist [llength $testlist]
1108 if {$nTestlist%3 || $nTestlist==0 } {
1109 error "SELECT test list contains [llength $testlist] elements"
1110 }
1111
danaf7626f2010-09-23 18:47:36 +00001112 eval $repair
dan51f06982010-09-18 19:00:12 +00001113 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +00001114 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +00001115 execsql $sql
dana16d1062010-09-28 17:37:28 +00001116 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
1117 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +00001118 set nRow 0
1119 db eval $sql {incr nRow}
1120 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
1121 } elseif {$errfmt==""} {
1122 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
1123 } else {
1124 set res [list 1 [string trim [format $errfmt {*}$res]]]
1125 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
1126 }
danaf7626f2010-09-23 18:47:36 +00001127 eval $repair
dan51f06982010-09-18 19:00:12 +00001128 }
danaf7626f2010-09-23 18:47:36 +00001129
dan51f06982010-09-18 19:00:12 +00001130}
1131
danb04757a2010-09-21 16:59:16 +00001132proc delete_all_data {} {
1133 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1134 db eval "DELETE FROM '[string map {' ''} $t]'"
1135 }
1136}
drh348784e2000-05-29 20:41:49 +00001137
mistachkin1d406e02013-08-29 01:09:14 +00001138# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001139# Return the number of microseconds per statement.
1140#
drh3590f152006-11-23 21:09:10 +00001141proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001142 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001143 flush stdout
1144 set speed [time {sqlite3_exec_nr db $sql}]
1145 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001146 if {$tm == 0} {
1147 set rate [format %20s "many"]
1148 } else {
1149 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1150 }
drh3590f152006-11-23 21:09:10 +00001151 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001152 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001153 global total_time
1154 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001155 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001156}
drh45c236d2008-03-22 01:08:00 +00001157proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001158 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001159 flush stdout
1160 set speed [time {eval $script}]
1161 set tm [lindex $speed 0]
1162 if {$tm == 0} {
1163 set rate [format %20s "many"]
1164 } else {
1165 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1166 }
1167 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001168 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001169 global total_time
1170 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001171 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001172}
drhdd924312007-03-31 22:34:16 +00001173proc speed_trial_init {name} {
1174 global total_time
1175 set total_time 0
dana975b592010-10-08 16:09:43 +00001176 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001177 sqlite3 versdb :memory:
1178 set vers [versdb one {SELECT sqlite_source_id()}]
1179 versdb close
dan17c08232015-06-09 15:58:28 +00001180 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001181}
1182proc speed_trial_summary {name} {
1183 global total_time
dan17c08232015-06-09 15:58:28 +00001184 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001185
1186 if { 0 } {
1187 sqlite3 versdb :memory:
1188 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1189 versdb close
dan17c08232015-06-09 15:58:28 +00001190 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001191 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001192 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001193 }
1194 }
drhb62c3352006-11-23 09:39:16 +00001195}
1196
drh348784e2000-05-29 20:41:49 +00001197# Run this routine last
1198#
1199proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001200 catch {db close}
dane8559832014-07-31 17:35:40 +00001201 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001202 catch {db2 close}
1203 catch {db3 close}
1204 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001205}
1206proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001207 global sqlite_open_file_count
1208
1209 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001210
drh6e142f52000-06-08 13:36:40 +00001211 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001212 catch {db2 close}
1213 catch {db3 close}
1214
drh9bc54492007-10-23 14:49:59 +00001215 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001216 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001217 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001218 db close
drh984bfaa2008-03-19 16:08:53 +00001219 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001220
drh31999c52019-11-14 17:46:32 +00001221 sqlite3_soft_heap_limit64 0
1222 sqlite3_hard_heap_limit64 0
danc1a60c52010-06-07 14:28:16 +00001223 set nTest [incr_ntest]
1224 set nErr [set_test_counter errors]
1225
drhef866372013-05-22 20:49:02 +00001226 set nKnown 0
1227 if {[file readable known-problems.txt]} {
1228 set fd [open known-problems.txt]
1229 set content [read $fd]
1230 close $fd
1231 foreach x $content {set known_error($x) 1}
1232 foreach x [set_test_counter fail_list] {
1233 if {[info exists known_error($x)]} {incr nKnown}
1234 }
1235 }
1236 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001237 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001238 out of $nTest tests"
1239 } else {
drh72bf6a32016-01-07 02:06:55 +00001240 set cpuinfo {}
1241 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1242 append cpuinfo " $::tcl_platform(os)"
1243 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1244 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1245 output2 "SQLite [sqlite3 -sourceid]"
1246 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001247 }
1248 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001249 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001250 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001251 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001252 }
dan17c08232015-06-09 15:58:28 +00001253 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001254 }
drhcca17c32013-04-19 12:32:52 +00001255 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001256 output2 "Warning: $warning"
drhcca17c32013-04-19 12:32:52 +00001257 }
danielk1977ee8b7992009-03-26 17:13:06 +00001258 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001259 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001260 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001261 set prec {}
1262 foreach {rec} [lsort $omitList] {
1263 if {$rec==$prec} continue
1264 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001265 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001266 }
1267 }
drh80788d82006-09-02 14:50:23 +00001268 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001269 output2 "******************************************************************"
1270 output2 "N.B.: The version of TCL that you used to build this test harness"
1271 output2 "is defective in that it does not support 64-bit integers. Some or"
1272 output2 "all of the test failures above might be a result from this defect"
1273 output2 "in your TCL build."
1274 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001275 }
danc1a60c52010-06-07 14:28:16 +00001276 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001277 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001278 }
drh94e92032003-02-16 22:21:32 +00001279 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001280 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001281 incr nErr
1282 }
drheafc43b2010-07-26 18:43:40 +00001283 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1284 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001285 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001286 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001287 incr nErr
dan253c6ee2018-09-18 17:00:06 +00001288 ifcapable mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001289 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001290 sqlite3_memdebug_dump ./memleak.txt
1291 }
drhf3a65f72007-08-22 20:18:21 +00001292 } else {
dan17c08232015-06-09 15:58:28 +00001293 output2 "All memory allocations freed - no leaks"
dan253c6ee2018-09-18 17:00:06 +00001294 ifcapable mem5 {
drhd2bb3272007-10-15 19:34:32 +00001295 sqlite3_memdebug_dump ./memusage.txt
1296 }
drhf3a65f72007-08-22 20:18:21 +00001297 }
drhf7141992008-06-19 00:16:08 +00001298 show_memstats
dan17c08232015-06-09 15:58:28 +00001299 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1300 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001301 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001302 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001303 }
danc1a60c52010-06-07 14:28:16 +00001304 if {$::cmdlinearg(malloctrace)} {
dan253c6ee2018-09-18 17:00:06 +00001305 output2 "Writing mallocs.tcl..."
1306 memdebug_log_sql mallocs.tcl
danielk197735754ac2008-03-21 17:29:37 +00001307 sqlite3_memdebug_log stop
1308 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001309 if {[sqlite3_memory_used]>0} {
dan253c6ee2018-09-18 17:00:06 +00001310 output2 "Writing leaks.tcl..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001311 sqlite3_memdebug_log sync
dan253c6ee2018-09-18 17:00:06 +00001312 memdebug_log_sql leaks.tcl
danielk1977dbdc4d42008-03-28 07:42:53 +00001313 }
danielk197735754ac2008-03-21 17:29:37 +00001314 }
dan1d07f1d2019-04-01 17:24:20 +00001315 if {[info commands vdbe_coverage]!=""} {
1316 vdbe_coverage_report
1317 }
drhd9910fe2006-10-04 11:55:49 +00001318 foreach f [glob -nocomplain test.db-*-journal] {
mistachkinfda06be2011-08-02 00:57:34 +00001319 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001320 }
1321 foreach f [glob -nocomplain test.db-mj*] {
mistachkinfda06be2011-08-02 00:57:34 +00001322 forcedelete $f
drhd9910fe2006-10-04 11:55:49 +00001323 }
drhdb25e382001-03-15 18:21:22 +00001324 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001325}
1326
dan1d07f1d2019-04-01 17:24:20 +00001327proc vdbe_coverage_report {} {
1328 puts "Writing vdbe coverage report to vdbe_coverage.txt"
1329 set lSrc [list]
1330 set iLine 0
1331 if {[file exists ../sqlite3.c]} {
1332 set fd [open ../sqlite3.c]
1333 set iLine
1334 while { ![eof $fd] } {
1335 set line [gets $fd]
1336 incr iLine
1337 if {[regexp {^/\** Begin file (.*\.c) \**/} $line -> file]} {
1338 lappend lSrc [list $iLine $file]
1339 }
1340 }
1341 close $fd
1342 }
1343 set fd [open vdbe_coverage.txt w]
1344 foreach miss [vdbe_coverage report] {
danafb3f3c2019-04-01 18:43:09 +00001345 foreach {line branch never} $miss {}
dan1d07f1d2019-04-01 17:24:20 +00001346 set nextfile ""
1347 while {[llength $lSrc]>0 && [lindex $lSrc 0 0] < $line} {
1348 set nextfile [lindex $lSrc 0 1]
1349 set lSrc [lrange $lSrc 1 end]
1350 }
1351 if {$nextfile != ""} {
1352 puts $fd ""
1353 puts $fd "### $nextfile ###"
1354 }
danafb3f3c2019-04-01 18:43:09 +00001355 puts $fd "Vdbe branch $line: never $never (path $branch)"
dan1d07f1d2019-04-01 17:24:20 +00001356 }
1357 close $fd
1358}
1359
drhf7141992008-06-19 00:16:08 +00001360# Display memory statistics for analysis and debugging purposes.
1361#
1362proc show_memstats {} {
1363 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001364 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1365 set val [format {now %10d max %10d max-size %10d} \
1366 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001367 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001368 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1369 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001370 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001371 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001372 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1373 set val [format {now %10d max %10d max-size %10d} \
1374 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001375 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001376 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1377 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001378 output1 "Page-cache overflow: $val"
drhec424a52008-07-25 15:39:03 +00001379 ifcapable yytrackmaxstackdepth {
1380 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1381 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001382 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001383 }
drhf7141992008-06-19 00:16:08 +00001384}
1385
drh348784e2000-05-29 20:41:49 +00001386# A procedure to execute SQL
1387#
drhc4a3c772001-04-04 11:48:57 +00001388proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001389 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001390 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001391}
drh3c2aeae2014-01-24 14:37:44 +00001392proc execsql_timed {sql {db db}} {
1393 set tm [time {
1394 set x [uplevel [list $db eval $sql]]
1395 } 1]
1396 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001397 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001398 set x
1399}
drh3aadb2e2000-05-31 17:59:25 +00001400
drhadbca9c2001-09-27 15:11:53 +00001401# Execute SQL and catch exceptions.
1402#
1403proc catchsql {sql {db db}} {
1404 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001405 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001406 lappend r $msg
1407 return $r
1408}
1409
drh04096482001-11-09 22:41:44 +00001410# Do an VDBE code dump on the SQL given
1411#
1412proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001413 output2 ""
1414 output2 "addr opcode p1 p2 p3 p4 p5 #"
1415 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001416 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001417 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001418 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1419 ]
drh04096482001-11-09 22:41:44 +00001420 }
1421}
1422
dane83267d2013-11-05 14:19:22 +00001423proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001424 output2 ""
1425 output2 "addr opcode p1 p2 p3 p4 p5 #"
1426 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001427
dane83267d2013-11-05 14:19:22 +00001428
dan5c663c32013-11-06 14:52:40 +00001429 # Set up colors for the different opcodes. Scheme is as follows:
1430 #
1431 # Red: Opcodes that write to a b-tree.
1432 # Blue: Opcodes that reposition or seek a cursor.
1433 # Green: The ResultRow opcode.
1434 #
dandea63f22014-03-03 16:48:47 +00001435 if { [catch {fconfigure stdout -mode}]==0 } {
1436 set R "\033\[31;1m" ;# Red fg
1437 set G "\033\[32;1m" ;# Green fg
1438 set B "\033\[34;1m" ;# Red fg
1439 set D "\033\[39;0m" ;# Default fg
1440 } else {
1441 set R ""
1442 set G ""
1443 set B ""
1444 set D ""
1445 }
dan5c663c32013-11-06 14:52:40 +00001446 foreach opcode {
dan8da209b2016-07-26 18:06:08 +00001447 Seek SeekGE SeekGT SeekLE SeekLT NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001448 NoConflict Next Prev VNext VPrev VFilter
dan8da209b2016-07-26 18:06:08 +00001449 SorterSort SorterNext NextIfOpen
dan5c663c32013-11-06 14:52:40 +00001450 } {
1451 set color($opcode) $B
1452 }
1453 foreach opcode {ResultRow} {
1454 set color($opcode) $G
1455 }
1456 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1457 set color($opcode) $R
1458 }
1459
dan427ebba2013-11-05 16:39:31 +00001460 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001461 $db eval "explain $sql" {} {
1462 set x($addr) 0
1463 set op($addr) $opcode
1464
dan427ebba2013-11-05 16:39:31 +00001465 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1466 set linebreak($p2) 1
1467 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001468 }
1469
dan8da209b2016-07-26 18:06:08 +00001470 if {$opcode=="Once"} {
1471 for {set i $addr} {$i<$p2} {incr i} {
1472 set star($i) $addr
1473 }
1474 }
1475
dane3ab7292013-11-12 12:30:09 +00001476 if {$opcode=="Next" || $opcode=="Prev"
1477 || $opcode=="VNext" || $opcode=="VPrev"
dan8da209b2016-07-26 18:06:08 +00001478 || $opcode=="SorterNext" || $opcode=="NextIfOpen"
dane3ab7292013-11-12 12:30:09 +00001479 } {
dane83267d2013-11-05 14:19:22 +00001480 for {set i $p2} {$i<$addr} {incr i} {
1481 incr x($i) 2
1482 }
1483 }
1484
1485 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1486 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1487 incr x($i) 2
1488 }
1489 }
dan427ebba2013-11-05 16:39:31 +00001490
1491 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1492 set linebreak([expr $addr+1]) 1
1493 }
dane83267d2013-11-05 14:19:22 +00001494 }
1495
1496 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001497 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001498 output2 ""
dane83267d2013-11-05 14:19:22 +00001499 }
1500 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001501
dan8da209b2016-07-26 18:06:08 +00001502 if {[info exists star($addr)]} {
1503 set ii [expr $x($star($addr))]
1504 append I " "
1505 set I [string replace $I $ii $ii *]
1506 }
1507
dan5c663c32013-11-06 14:52:40 +00001508 set col ""
1509 catch { set col $color($opcode) }
1510
mistachkineeb31ff2015-06-10 23:02:38 +00001511 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001512 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001513 ]
dane83267d2013-11-05 14:19:22 +00001514 }
mistachkineeb31ff2015-06-10 23:02:38 +00001515 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001516}
1517
drhdafc0ce2008-04-17 19:14:02 +00001518# Show the VDBE program for an SQL statement but omit the Trace
1519# opcode at the beginning. This procedure can be used to prove
1520# that different SQL statements generate exactly the same VDBE code.
1521#
1522proc explain_no_trace {sql} {
1523 set tr [db eval "EXPLAIN $sql"]
1524 return [lrange $tr 7 end]
1525}
1526
drh3aadb2e2000-05-31 17:59:25 +00001527# Another procedure to execute SQL. This one includes the field
1528# names in the returned list.
1529#
1530proc execsql2 {sql} {
1531 set result {}
1532 db eval $sql data {
1533 foreach f $data(*) {
1534 lappend result $f $data($f)
1535 }
1536 }
1537 return $result
1538}
drh17a68932001-01-31 13:28:08 +00001539
mistachkin1d406e02013-08-29 01:09:14 +00001540# Use a temporary in-memory database to execute SQL statements
1541#
1542proc memdbsql {sql} {
1543 sqlite3 memdb :memory:
1544 set result [memdb eval $sql]
1545 memdb close
1546 return $result
1547}
1548
drhdde85d92003-03-01 19:45:34 +00001549# Use the non-callback API to execute multiple SQL statements
1550#
1551proc stepsql {dbptr sql} {
1552 set sql [string trim $sql]
1553 set r 0
1554 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001555 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001556 return [list 1 $vm]
1557 }
1558 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001559# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1560# foreach v $VAL {lappend r $v}
1561# }
1562 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1563 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1564 lappend r [sqlite3_column_text $vm $i]
1565 }
drhdde85d92003-03-01 19:45:34 +00001566 }
danielk1977106bb232004-05-21 10:08:53 +00001567 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001568 return [list 1 $errmsg]
1569 }
1570 }
1571 return $r
1572}
1573
drh21504322002-06-25 13:16:02 +00001574# Do an integrity check of the entire database
1575#
danielk197704103022009-02-03 16:51:24 +00001576proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001577 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001578 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001579 }
1580}
1581
drh433dccf2013-02-09 15:37:11 +00001582# Check the extended error code
1583#
1584proc verify_ex_errcode {name expected {db db}} {
1585 do_test $name [list sqlite3_extended_errcode $db] $expected
1586}
1587
danc6055c72011-04-28 18:46:46 +00001588
1589# Return true if the SQL statement passed as the second argument uses a
1590# statement transaction.
1591#
1592proc sql_uses_stmt {db sql} {
1593 set stmt [sqlite3_prepare $db $sql -1 dummy]
1594 set uses [uses_stmt_journal $stmt]
1595 sqlite3_finalize $stmt
1596 return $uses
1597}
1598
danielk1977db4e8862008-01-16 08:24:46 +00001599proc fix_ifcapable_expr {expr} {
1600 set ret ""
1601 set state 0
1602 for {set i 0} {$i < [string length $expr]} {incr i} {
1603 set char [string range $expr $i $i]
1604 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1605 if {$newstate && !$state} {
1606 append ret {$::sqlite_options(}
1607 }
1608 if {!$newstate && $state} {
1609 append ret )
1610 }
1611 append ret $char
1612 set state $newstate
1613 }
1614 if {$state} {append ret )}
1615 return $ret
1616}
1617
mistachkinc60941f2012-09-13 01:51:02 +00001618# Returns non-zero if the capabilities are present; zero otherwise.
1619#
1620proc capable {expr} {
1621 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1622}
1623
drh27d258a2004-10-30 20:23:09 +00001624# Evaluate a boolean expression of capabilities. If true, execute the
1625# code. Omit the code if false.
1626#
danielk19773e8c37e2005-01-21 03:12:14 +00001627proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001628 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1629 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001630 if ($e2) {
1631 set c [catch {uplevel 1 $code} r]
1632 } else {
1633 set c [catch {uplevel 1 $elsecode} r]
1634 }
1635 return -code $c $r
drh21504322002-06-25 13:16:02 +00001636}
danielk197745901d62004-11-10 15:27:38 +00001637
danielk1977aca790a2005-01-13 11:07:52 +00001638# This proc execs a seperate process that crashes midway through executing
1639# the SQL script $sql on database test.db.
1640#
1641# The crash occurs during a sync() of file $crashfile. When the crash
1642# occurs a random subset of all unsynced writes made by the process are
1643# written into the files on disk. Argument $crashdelay indicates the
1644# number of file syncs to wait before crashing.
1645#
1646# The return value is a list of two elements. The first element is a
1647# boolean, indicating whether or not the process actually crashed or
1648# reported some other error. The second element in the returned list is the
1649# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001650# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001651#
danielk1977f8940ae2007-08-23 11:07:10 +00001652# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001653#
1654proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001655
1656 set blocksize ""
1657 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001658 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001659 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001660 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001661 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001662 set dc ""
dancb1b0a62017-03-02 14:51:47 +00001663 set dfltvfs 0
danielk197759a33f92007-03-17 10:26:59 +00001664 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001665
danielk197759a33f92007-03-17 10:26:59 +00001666 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1667 set z [lindex $args $ii]
1668 set n [string length $z]
1669 set z2 [lindex $args [expr $ii+1]]
1670
1671 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001672 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001673 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001674 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001675 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001676 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
dancb1b0a62017-03-02 14:51:47 +00001677 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" }\
1678 elseif {$n>1 && [string first $z -dfltvfs]==0} {set dfltvfs $z2 }\
danielk197759a33f92007-03-17 10:26:59 +00001679 else { error "Unrecognized option: $z" }
1680 }
1681
1682 if {$crashfile eq ""} {
1683 error "Compulsory option -file missing"
1684 }
1685
mistachkin1d406e02013-08-29 01:09:14 +00001686 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001687 # cfSync(), which can be different then what TCL uses by
1688 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001689 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001690
1691 set f [open crash.tcl w]
dancb1b0a62017-03-02 14:51:47 +00001692 puts $f "sqlite3_crash_enable 1 $dfltvfs"
danielk1977f8940ae2007-08-23 11:07:10 +00001693 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001694 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001695
1696 # This block sets the cache size of the main database to 10
1697 # pages. This is done in case the build is configured to omit
1698 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001699 if {$opendb!=""} {
1700 puts $f $opendb
1701 puts $f {db eval {SELECT * FROM sqlite_master;}}
1702 puts $f {set bt [btree_from_db db]}
1703 puts $f {btree_set_cache_size $bt 10}
1704 }
dan999cd082013-12-09 20:42:03 +00001705
drhcb1f0f62008-01-08 15:18:52 +00001706 if {$prngseed} {
1707 set seed [expr {$prngseed%10007+1}]
1708 # puts seed=$seed
1709 puts $f "db eval {SELECT randomblob($seed)}"
1710 }
danielk1977933bbd62007-03-17 07:22:42 +00001711
drhf8587402008-01-08 16:03:49 +00001712 if {[string length $tclbody]>0} {
1713 puts $f $tclbody
1714 }
1715 if {[string length $sql]>0} {
1716 puts $f "db eval {"
1717 puts $f "$sql"
1718 puts $f "}"
1719 }
danielk1977aca790a2005-01-13 11:07:52 +00001720 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001721 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001722 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001723 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001724
shanehf2c08822010-07-08 16:30:44 +00001725 # Windows/ActiveState TCL returns a slightly different
1726 # error message. We map that to the expected message
1727 # so that we don't have to change all of the test
1728 # cases.
1729 if {$::tcl_platform(platform)=="windows"} {
1730 if {$msg=="child killed: unknown signal"} {
1731 set msg "child process exited abnormally"
1732 }
1733 }
mistachkin1d406e02013-08-29 01:09:14 +00001734
danielk1977aca790a2005-01-13 11:07:52 +00001735 lappend r $msg
1736}
1737
dan33447e72017-07-22 20:12:31 +00001738# crash_on_write ?-devchar DEVCHAR? CRASHDELAY SQL
1739#
1740proc crash_on_write {args} {
1741
1742 set nArg [llength $args]
1743 if {$nArg<2 || $nArg%2} {
1744 error "bad args: $args"
1745 }
1746 set zSql [lindex $args end]
1747 set nDelay [lindex $args end-1]
1748
1749 set devchar {}
1750 for {set ii 0} {$ii < $nArg-2} {incr ii 2} {
1751 set opt [lindex $args $ii]
1752 switch -- [lindex $args $ii] {
1753 -devchar {
1754 set devchar [lindex $args [expr $ii+1]]
1755 }
1756
1757 default { error "unrecognized option: $opt" }
1758 }
1759 }
1760
1761 set f [open crash.tcl w]
1762 puts $f "sqlite3_crash_on_write $nDelay"
1763 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
1764 puts $f "sqlite3 db test.db -vfs writecrash"
1765 puts $f "db eval {$zSql}"
1766 puts $f "set {} {}"
1767
1768 close $f
1769 set r [catch {
1770 exec [info nameofexec] crash.tcl >@stdout
1771 } msg]
1772
1773 # Windows/ActiveState TCL returns a slightly different
1774 # error message. We map that to the expected message
1775 # so that we don't have to change all of the test
1776 # cases.
1777 if {$::tcl_platform(platform)=="windows"} {
1778 if {$msg=="child killed: unknown signal"} {
1779 set msg "child process exited abnormally"
1780 }
1781 }
1782
1783 lappend r $msg
1784}
1785
danb88e24f2013-02-23 18:58:11 +00001786proc run_ioerr_prep {} {
1787 set ::sqlite_io_error_pending 0
1788 catch {db close}
1789 catch {db2 close}
1790 catch {forcedelete test.db}
1791 catch {forcedelete test.db-journal}
1792 catch {forcedelete test2.db}
1793 catch {forcedelete test2.db-journal}
1794 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1795 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1796 if {[info exists ::ioerropts(-tclprep)]} {
1797 eval $::ioerropts(-tclprep)
1798 }
1799 if {[info exists ::ioerropts(-sqlprep)]} {
1800 execsql $::ioerropts(-sqlprep)
1801 }
1802 expr 0
1803}
1804
danielk197732554c12005-01-22 03:39:39 +00001805# Usage: do_ioerr_test <test number> <options...>
1806#
1807# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001808# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001809# used to name the tests executed by this proc. Options are as follows:
1810#
1811# -tclprep TCL script to run to prepare test.
1812# -sqlprep SQL script to run to prepare test.
1813# -tclbody TCL script to run with IO error simulation.
1814# -sqlbody TCL script to run with IO error simulation.
1815# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001816# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001817# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001818# -start Value of 'N' to begin with (default 1)
1819#
1820# -cksum Boolean. If true, test that the database does
1821# not change during the execution of the test case.
1822#
1823proc do_ioerr_test {testname args} {
1824
danielk197732554c12005-01-22 03:39:39 +00001825 set ::ioerropts(-start) 1
1826 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001827 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001828 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001829 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001830 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001831 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001832 array set ::ioerropts $args
1833
danielk197747cd39c2008-05-12 12:41:15 +00001834 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1835 # a couple of obscure IO errors that do not return them.
1836 set ::ioerropts(-erc) 0
mistachkin1d406e02013-08-29 01:09:14 +00001837
danb88e24f2013-02-23 18:58:11 +00001838 # Create a single TCL script from the TCL and SQL specified
1839 # as the body of the test.
1840 set ::ioerrorbody {}
1841 if {[info exists ::ioerropts(-tclbody)]} {
1842 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1843 }
1844 if {[info exists ::ioerropts(-sqlbody)]} {
1845 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1846 }
1847
1848 save_prng_state
1849 if {$::ioerropts(-cksum)} {
1850 run_ioerr_prep
1851 eval $::ioerrorbody
1852 set ::goodcksum [cksum]
1853 }
danielk197747cd39c2008-05-12 12:41:15 +00001854
danielk197732554c12005-01-22 03:39:39 +00001855 set ::go 1
drh93aed5a2008-01-16 17:46:38 +00001856 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001857 for {set n $::ioerropts(-start)} {$::go} {incr n} {
danielk197792d4d7a2007-05-04 12:05:56 +00001858 set ::TN $n
drhc2ee76c2007-01-04 14:58:14 +00001859 incr ::ioerropts(-count) -1
1860 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001861
danielk197732554c12005-01-22 03:39:39 +00001862 # Skip this IO error if it was specified with the "-exclude" option.
1863 if {[info exists ::ioerropts(-exclude)]} {
1864 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1865 }
danielk1977861f7452008-06-05 11:39:11 +00001866 if {$::ioerropts(-restoreprng)} {
1867 restore_prng_state
1868 }
danielk197732554c12005-01-22 03:39:39 +00001869
mistachkin1d406e02013-08-29 01:09:14 +00001870 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001871 # SQL (in that order) to prepare for the test case.
1872 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001873 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001874 } {0}
1875
1876 # Read the 'checksum' of the database.
1877 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001878 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001879 }
drh93aed5a2008-01-16 17:46:38 +00001880
danielk197732554c12005-01-22 03:39:39 +00001881 # Set the Nth IO error to fail.
1882 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001883 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001884 set ::sqlite_io_error_pending $n
1885 }] $n
danielk197732554c12005-01-22 03:39:39 +00001886
danb88e24f2013-02-23 18:58:11 +00001887 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001888 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001889 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001890 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001891 set ::sqlite_io_error_hit 0
1892 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001893 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001894 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001895 set rc [sqlite3_errcode $::DB]
1896 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001897 # If we are in extended result code mode, make sure all of the
1898 # IOERRs we get back really do have their extended code values.
1899 # If an extended result code is returned, the sqlite3_errcode
1900 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1901 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001902 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1903 return $rc
1904 }
1905 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001906 # If we are not in extended result code mode, make sure no
1907 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001908 if {[regexp {\+\d} $rc]} {
1909 return $rc
1910 }
1911 }
drh93aed5a2008-01-16 17:46:38 +00001912 # The test repeats as long as $::go is non-zero. $::go starts out
1913 # as 1. When a test runs to completion without hitting an I/O
1914 # error, that means there is no point in continuing with this test
1915 # case so set $::go to zero.
1916 #
1917 if {$::sqlite_io_error_pending>0} {
1918 set ::go 0
1919 set q 0
1920 set ::sqlite_io_error_pending 0
1921 } else {
1922 set q 1
1923 }
1924
drhc9ac5ca2005-11-04 22:03:30 +00001925 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001926 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1927 set r 1
1928 }
danielk1977f2fa8312006-01-24 13:09:33 +00001929 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001930
1931 # One of two things must have happened. either
1932 # 1. We never hit the IO error and the SQL returned OK
1933 # 2. An IO error was hit and the SQL failed
1934 #
dand41a29a2010-05-06 15:56:28 +00001935 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001936 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001937 } {1}
1938
danielk197780daec62008-05-12 10:57:02 +00001939 set ::sqlite_io_error_hit 0
1940 set ::sqlite_io_error_pending 0
1941
mistachkin1d406e02013-08-29 01:09:14 +00001942 # Check that no page references were leaked. There should be
1943 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001944 # or zero otherwise.
1945 #
danielk197781620542008-06-07 05:19:37 +00001946 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001947 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001948 # occurs while attempting to detect a hot-journal file), then
1949 # there may 0 page references and an active transaction according
1950 # to [sqlite3_get_autocommit].
1951 #
danielk19774abd5442008-05-05 15:26:50 +00001952 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001953 do_test $testname.$n.4 {
1954 set bt [btree_from_db db]
1955 db_enter db
1956 array set stats [btree_pager_stats $bt]
1957 db_leave db
danielk197781620542008-06-07 05:19:37 +00001958 set nRef $stats(ref)
1959 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1960 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001961 }
1962
mistachkin1d406e02013-08-29 01:09:14 +00001963 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001964 # and the pager is not running in exclusive-locking mode,
1965 # check that the pager is in "unlocked" state. Theoretically,
1966 # if a call to xUnlock() failed due to an IO error the underlying
1967 # file may still be locked.
1968 #
1969 ifcapable pragma {
1970 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001971 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001972 && [db one {pragma locking_mode}] eq "normal"
1973 && [sqlite3_get_autocommit db]
1974 } {
1975 do_test $testname.$n.5 {
1976 set bt [btree_from_db db]
1977 db_enter db
1978 array set stats [btree_pager_stats $bt]
1979 db_leave db
1980 set stats(state)
1981 } 0
1982 }
1983 }
1984
mistachkin48864df2013-03-21 21:20:32 +00001985 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001986 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001987 #
drh1aa5af12008-03-07 19:51:14 +00001988 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001989 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001990 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001991 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001992 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001993 set nowcksum [cksum]
1994 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1995 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001996 output2 "now=$nowcksum"
1997 output2 "the=$::checksum"
1998 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001999 }
2000 set res
2001 } 1
danielk197732554c12005-01-22 03:39:39 +00002002 }
2003
drh1aa5af12008-03-07 19:51:14 +00002004 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00002005 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00002006 if {[info exists ::ioerropts(-cleanup)]} {
2007 catch $::ioerropts(-cleanup)
2008 }
danielk197732554c12005-01-22 03:39:39 +00002009 }
2010 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00002011 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00002012 unset ::ioerropts
2013}
2014
drh93aed5a2008-01-16 17:46:38 +00002015# Return a checksum based on the contents of the main database associated
2016# with connection $db
danielk197732554c12005-01-22 03:39:39 +00002017#
2018proc cksum {{db db}} {
2019 set txt [$db eval {
2020 SELECT name, type, sql FROM sqlite_master order by name
2021 }]\n
2022 foreach tbl [$db eval {
2023 SELECT name FROM sqlite_master WHERE type='table' order by name
2024 }] {
2025 append txt [$db eval "SELECT * FROM $tbl"]\n
2026 }
2027 foreach prag {default_synchronous default_cache_size} {
2028 append txt $prag-[$db eval "PRAGMA $prag"]\n
2029 }
2030 set cksum [string length $txt]-[md5 $txt]
2031 # puts $cksum-[file size test.db]
2032 return $cksum
2033}
2034
drh93aed5a2008-01-16 17:46:38 +00002035# Generate a checksum based on the contents of the main and temp tables
2036# database $db. If the checksum of two databases is the same, and the
2037# integrity-check passes for both, the two databases are identical.
2038#
drh1db639c2008-01-17 02:36:28 +00002039proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00002040 set ret [list]
2041 ifcapable tempdb {
2042 set sql {
2043 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2044 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
2045 SELECT 'sqlite_master' UNION
2046 SELECT 'sqlite_temp_master' ORDER BY 1
2047 }
2048 } else {
2049 set sql {
2050 SELECT name FROM sqlite_master WHERE type = 'table' UNION
2051 SELECT 'sqlite_master' ORDER BY 1
2052 }
2053 }
2054 set tbllist [$db eval $sql]
2055 set txt {}
2056 foreach tbl $tbllist {
2057 append txt [$db eval "SELECT * FROM $tbl"]
2058 }
2059 foreach prag {default_cache_size} {
2060 append txt $prag-[$db eval "PRAGMA $prag"]\n
2061 }
2062 # puts txt=$txt
2063 return [md5 $txt]
2064}
2065
drhdc2c4912009-02-04 22:46:47 +00002066# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00002067# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00002068# Examples of $dbname are "temp" or "main".
2069#
2070proc dbcksum {db dbname} {
2071 if {$dbname=="temp"} {
2072 set master sqlite_temp_master
2073 } else {
2074 set master $dbname.sqlite_master
2075 }
2076 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
2077 set txt [$db eval "SELECT * FROM $master"]\n
2078 foreach tab $alltab {
2079 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
2080 }
2081 return [md5 $txt]
2082}
2083
dan253c6ee2018-09-18 17:00:06 +00002084proc memdebug_log_sql {filename} {
danielk197735754ac2008-03-21 17:29:37 +00002085
danielk19776f332c12008-03-21 14:22:44 +00002086 set data [sqlite3_memdebug_log dump]
2087 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00002088 if {$nFrame < 0} { return "" }
2089
danielk197735754ac2008-03-21 17:29:37 +00002090 set database temp
2091
danielk19776ab3a2e2009-02-19 14:39:25 +00002092 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00002093
2094 set sql ""
2095 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00002096 set nCall [lindex $e 0]
2097 set nByte [lindex $e 1]
2098 set lStack [lrange $e 2 end]
2099 append sql "INSERT INTO ${database}.malloc VALUES"
2100 append sql "('test', $nCall, $nByte, '$lStack');\n"
2101 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00002102 set frames($f) 1
2103 }
2104 }
2105
2106 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00002107 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00002108
dandee9be92019-02-05 16:53:26 +00002109 set pid [pid]
2110
danielk19776f332c12008-03-21 14:22:44 +00002111 foreach f [array names frames] {
2112 set addr [format %x $f]
dandee9be92019-02-05 16:53:26 +00002113 set cmd "eu-addr2line --pid=$pid $addr"
danielk19776f332c12008-03-21 14:22:44 +00002114 set line [eval exec $cmd]
2115 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00002116
2117 set file [lindex [split $line :] 0]
2118 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00002119 }
2120
danielk197735754ac2008-03-21 17:29:37 +00002121 foreach f [array names files] {
2122 set contents ""
2123 catch {
2124 set fd [open $f]
2125 set contents [read $fd]
2126 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002127 }
danielk197735754ac2008-03-21 17:29:37 +00002128 set contents [string map {' ''} $contents]
2129 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00002130 }
danielk19776f332c12008-03-21 14:22:44 +00002131
dan253c6ee2018-09-18 17:00:06 +00002132 set escaped "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
2133 set escaped [string map [list "{" "\\{" "}" "\\}"] $escaped]
2134
danielk197735754ac2008-03-21 17:29:37 +00002135 set fd [open $filename w]
dan253c6ee2018-09-18 17:00:06 +00002136 puts $fd "set BUILTIN {"
2137 puts $fd $escaped
2138 puts $fd "}"
2139 puts $fd {set BUILTIN [string map [list "\\{" "{" "\\}" "}"] $BUILTIN]}
2140 set mtv [open $::testdir/malloctraceviewer.tcl]
2141 set txt [read $mtv]
2142 close $mtv
2143 puts $fd $txt
danielk197735754ac2008-03-21 17:29:37 +00002144 close $fd
danielk19776f332c12008-03-21 14:22:44 +00002145}
drh93aed5a2008-01-16 17:46:38 +00002146
danf5894502009-10-07 18:41:19 +00002147# Drop all tables in database [db]
2148proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00002149 ifcapable trigger&&foreignkey {
2150 set pk [$db one "PRAGMA foreign_keys"]
2151 $db eval "PRAGMA foreign_keys = OFF"
2152 }
drh9a6ffc82010-02-15 18:03:20 +00002153 foreach {idx name file} [db eval {PRAGMA database_list}] {
2154 if {$idx==1} {
2155 set master sqlite_temp_master
2156 } else {
2157 set master $name.sqlite_master
2158 }
2159 foreach {t type} [$db eval "
2160 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00002161 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00002162 "] {
dana16d1062010-09-28 17:37:28 +00002163 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00002164 }
danf5894502009-10-07 18:41:19 +00002165 }
shaneh4e7b32f2009-12-17 22:12:51 +00002166 ifcapable trigger&&foreignkey {
2167 $db eval "PRAGMA foreign_keys = $pk"
2168 }
danf5894502009-10-07 18:41:19 +00002169}
2170
dand05a7142016-08-02 17:07:51 +00002171# Drop all auxiliary indexes from the main database opened by handle [db].
2172#
2173proc drop_all_indexes {{db db}} {
2174 set L [$db eval {
2175 SELECT name FROM sqlite_master WHERE type='index' AND sql LIKE 'create%'
2176 }]
2177 foreach idx $L { $db eval "DROP INDEX $idx" }
2178}
2179
2180
dan71cb5182010-04-26 12:39:03 +00002181#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00002182# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00002183# "wal", then the tests are run in WAL mode. Otherwise, they should be run
2184# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00002185# intrusive:
dan71cb5182010-04-26 12:39:03 +00002186#
2187# wal_set_journal_mode ?DB?
2188#
2189# If running a WAL test, execute "PRAGMA journal_mode = wal" using
2190# connection handle DB. Otherwise, this command is a no-op.
2191#
2192# wal_check_journal_mode TESTNAME ?DB?
2193#
2194# If running a WAL test, execute a tests case that fails if the main
2195# database for connection handle DB is not currently a WAL database.
2196# Otherwise (if not running a WAL permutation) this is a no-op.
2197#
2198# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00002199#
dan71cb5182010-04-26 12:39:03 +00002200# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00002201#
dan71cb5182010-04-26 12:39:03 +00002202proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00002203 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00002204}
2205proc wal_set_journal_mode {{db db}} {
2206 if { [wal_is_wal_mode] } {
2207 $db eval "PRAGMA journal_mode = WAL"
2208 }
2209}
2210proc wal_check_journal_mode {testname {db db}} {
2211 if { [wal_is_wal_mode] } {
2212 $db eval { SELECT * FROM sqlite_master }
2213 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
2214 }
2215}
2216
dan05accd22016-04-27 18:54:49 +00002217proc wal_is_capable {} {
2218 ifcapable !wal { return 0 }
2219 if {[permutation]=="journaltest"} { return 0 }
2220 return 1
2221}
2222
dan430e74c2010-06-07 17:47:26 +00002223proc permutation {} {
2224 set perm ""
2225 catch {set perm $::G(perm:name)}
2226 set perm
2227}
dancb79e512010-08-06 13:50:07 +00002228proc presql {} {
2229 set presql ""
2230 catch {set presql $::G(perm:presql)}
2231 set presql
2232}
dan430e74c2010-06-07 17:47:26 +00002233
danbe7721d2016-02-04 17:31:03 +00002234proc isquick {} {
2235 set ret 0
2236 catch {set ret $::G(isquick)}
2237 set ret
2238}
2239
danc1a60c52010-06-07 14:28:16 +00002240#-------------------------------------------------------------------------
2241#
2242proc slave_test_script {script} {
2243
2244 # Create the interpreter used to run the test script.
2245 interp create tinterp
2246
2247 # Populate some global variables that tester.tcl expects to see.
2248 foreach {var value} [list \
2249 ::argv0 $::argv0 \
2250 ::argv {} \
2251 ::SLAVE 1 \
2252 ] {
2253 interp eval tinterp [list set $var $value]
2254 }
2255
dan17c08232015-06-09 15:58:28 +00002256 # If output is being copied into a file, share the file-descriptor with
2257 # the interpreter.
2258 if {[info exists ::G(output_fd)]} {
2259 interp share {} $::G(output_fd) tinterp
2260 }
2261
danc1a60c52010-06-07 14:28:16 +00002262 # The alias used to access the global test counters.
2263 tinterp alias set_test_counter set_test_counter
2264
2265 # Set up the ::cmdlinearg array in the slave.
2266 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2267
dan430e74c2010-06-07 17:47:26 +00002268 # Set up the ::G array in the slave.
2269 interp eval tinterp [list array set ::G [array get ::G]]
2270
danc1a60c52010-06-07 14:28:16 +00002271 # Load the various test interfaces implemented in C.
2272 load_testfixture_extensions tinterp
2273
2274 # Run the test script.
2275 interp eval tinterp $script
2276
danea5542d2010-07-06 11:26:15 +00002277 # Check if the interpreter call [run_thread_tests]
2278 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2279 set ::run_thread_tests_called 1
2280 }
2281
danc1a60c52010-06-07 14:28:16 +00002282 # Delete the interpreter used to run the test script.
2283 interp delete tinterp
2284}
2285
dan430e74c2010-06-07 17:47:26 +00002286proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002287 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002288
dan6a64d672011-04-04 15:38:16 +00002289 if {[info exists ::G(start:permutation)]} {
2290 if {[permutation] != $::G(start:permutation)} return
2291 unset ::G(start:permutation)
2292 }
2293 if {[info exists ::G(start:file)]} {
2294 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2295 unset ::G(start:file)
2296 }
2297
dan92d516a2010-07-05 14:54:48 +00002298 # Remember the value of the shared-cache setting. So that it is possible
2299 # to check afterwards that it was not modified by the test script.
2300 #
2301 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002302
dan92d516a2010-07-05 14:54:48 +00002303 # Run the test script in a slave interpreter.
2304 #
danea5542d2010-07-06 11:26:15 +00002305 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002306 reset_prng_state
2307 set ::sqlite_open_file_count 0
2308 set time [time { slave_test_script [list source $zFile] }]
2309 set ms [expr [lindex $time 0] / 1000]
2310
dan92d516a2010-07-05 14:54:48 +00002311 # Test that all files opened by the test script were closed. Omit this
2312 # if the test script has "thread" in its name. The open file counter
2313 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002314 #
danea5542d2010-07-06 11:26:15 +00002315 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002316 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2317 }
dan430e74c2010-06-07 17:47:26 +00002318 set ::sqlite_open_file_count 0
2319
mistachkin1d406e02013-08-29 01:09:14 +00002320 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002321 # the test script.
2322 #
mistachkin1d406e02013-08-29 01:09:14 +00002323 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002324 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2325 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2326 }
dan430e74c2010-06-07 17:47:26 +00002327
dan92d516a2010-07-05 14:54:48 +00002328 # Add some info to the output.
2329 #
dan17c08232015-06-09 15:58:28 +00002330 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002331 show_memstats
danc1a60c52010-06-07 14:28:16 +00002332}
2333
dan59257dc2010-08-04 11:34:31 +00002334# Open a new connection on database test.db and execute the SQL script
2335# supplied as an argument. Before returning, close the new conection and
2336# restore the 4 byte fields starting at header offsets 28, 92 and 96
2337# to the values they held before the SQL was executed. This simulates
2338# a write by a pre-3.7.0 client.
2339#
2340proc sql36231 {sql} {
2341 set B [hexio_read test.db 92 8]
2342 set A [hexio_read test.db 28 4]
2343 sqlite3 db36231 test.db
2344 catch { db36231 func a_string a_string }
2345 execsql $sql db36231
2346 db36231 close
2347 hexio_write test.db 28 $A
2348 hexio_write test.db 92 $B
2349 return ""
2350}
danf5894502009-10-07 18:41:19 +00002351
danccc7ff42010-11-29 12:06:45 +00002352proc db_save {} {
2353 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2354 foreach f [glob -nocomplain test.db*] {
2355 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002356 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002357 }
2358}
2359proc db_save_and_close {} {
2360 db_save
2361 catch { db close }
2362 return ""
2363}
2364proc db_restore {} {
2365 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2366 foreach f2 [glob -nocomplain sv_test.db*] {
2367 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002368 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002369 }
2370}
2371proc db_restore_and_reopen {{dbfile test.db}} {
2372 catch { db close }
2373 db_restore
2374 sqlite3 db $dbfile
2375}
2376proc db_delete_and_reopen {{file test.db}} {
2377 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002378 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002379 sqlite3 db $file
2380}
2381
dan03bc5252015-07-24 14:17:17 +00002382# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2383# to configure the size of the PAGECACHE allocation using the parameters
2384# provided to this command. Save the old PAGECACHE parameters in a global
2385# variable so that [test_restore_config_pagecache] can restore the previous
2386# configuration.
2387#
2388# Before returning, reopen connection [db] on file test.db.
2389#
2390proc test_set_config_pagecache {sz nPg} {
2391 catch {db close}
2392 catch {db2 close}
2393 catch {db3 close}
2394
2395 sqlite3_shutdown
2396 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2397 sqlite3_initialize
2398 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002399 reset_db
dan03bc5252015-07-24 14:17:17 +00002400}
2401
2402# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2403# to configure the size of the PAGECACHE allocation to the size saved in
2404# the global variable by an earlier call to [test_set_config_pagecache].
2405#
2406# Before returning, reopen connection [db] on file test.db.
2407#
2408proc test_restore_config_pagecache {} {
2409 catch {db close}
2410 catch {db2 close}
2411 catch {db3 close}
2412
2413 sqlite3_shutdown
2414 eval sqlite3_config_pagecache $::old_pagecache_config
2415 unset ::old_pagecache_config
2416 sqlite3_initialize
2417 autoinstall_test_functions
2418 sqlite3 db test.db
2419}
2420
dan43efc182017-12-19 17:42:13 +00002421proc test_binary_name {nm} {
dan089555c2016-03-15 09:55:44 +00002422 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002423 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002424 } else {
dan1e8dae02016-03-19 14:53:36 +00002425 set ret $nm
dan089555c2016-03-15 09:55:44 +00002426 }
dan43efc182017-12-19 17:42:13 +00002427 file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]
2428}
2429
2430proc test_find_binary {nm} {
2431 set ret [test_binary_name $nm]
dan089555c2016-03-15 09:55:44 +00002432 if {![file executable $ret]} {
2433 finish_test
dan49aed582016-03-19 15:13:59 +00002434 return ""
dan089555c2016-03-15 09:55:44 +00002435 }
2436 return $ret
2437}
2438
danielk197745901d62004-11-10 15:27:38 +00002439# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2440# the tests in shell[1-5].test. If no such executable can be found, invoke
2441# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002442#
dan64b95bb2012-05-12 05:30:29 +00002443proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002444 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002445 if {$prog==""} { return -code return }
2446 return $prog
drh348784e2000-05-29 20:41:49 +00002447}
2448
dan1e8dae02016-03-19 14:53:36 +00002449# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2450# the tests in sqldiff tests. If no such executable can be found, invoke
2451# [finish_test ; return] in the callers context.
2452#
2453proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002454 set prog [test_find_binary sqldiff]
2455 if {$prog==""} { return -code return }
2456 return $prog
dan1e8dae02016-03-19 14:53:36 +00002457}
2458
daneab0e102018-02-07 18:02:50 +00002459# Call sqlite3_expanded_sql() on all statements associated with database
2460# connection $db. This sometimes finds use-after-free bugs if run with
2461# valgrind or address-sanitizer.
2462proc expand_all_sql {db} {
2463 set stmt ""
2464 while {[set stmt [sqlite3_next_stmt $db $stmt]]!=""} {
2465 sqlite3_expanded_sql $stmt
2466 }
2467}
2468
dan1e8dae02016-03-19 14:53:36 +00002469
drh348784e2000-05-29 20:41:49 +00002470# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2471# to non-zero, then set the global variable $AUTOVACUUM to 1.
2472set AUTOVACUUM $sqlite_options(default_autovacuum)
2473
dan64b95bb2012-05-12 05:30:29 +00002474# Make sure the FTS enhanced query syntax is disabled.
2475set sqlite_fts3_enable_parentheses 0
2476
drh348784e2000-05-29 20:41:49 +00002477# During testing, assume that all database files are well-formed. The
2478# few test cases that deliberately corrupt database files should rescind
2479# this setting by invoking "database_can_be_corrupt"
2480#
2481database_never_corrupt
drhca439a42020-07-22 21:05:23 +00002482extra_schema_checks 1
drh348784e2000-05-29 20:41:49 +00002483
drh348784e2000-05-29 20:41:49 +00002484source $testdir/thread_common.tcl
2485source $testdir/malloc_common.tcl