blob: 19bd601df6e76c83a2d7905e0a3a86768e58b747 [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
dan6f343962011-07-01 18:26:40 +000027# drop_all_tables ?DB?
mistachkinfda06be2011-08-02 00:57:34 +000028# forcecopy FROM TO
danc1a60c52010-06-07 14:28:16 +000029# forcedelete FILENAME
30#
31# Test the capability of the SQLite version built into the interpreter to
32# determine if a specific test can be run:
33#
mistachkinc60941f2012-09-13 01:51:02 +000034# capable EXPR
danc1a60c52010-06-07 14:28:16 +000035# ifcapable EXPR
36#
37# Calulate checksums based on database contents:
38#
39# dbcksum DB DBNAME
40# allcksum ?DB?
41# cksum ?DB?
42#
43# Commands to execute/explain SQL statements:
44#
mistachkin1d406e02013-08-29 01:09:14 +000045# memdbsql SQL
danc1a60c52010-06-07 14:28:16 +000046# stepsql DB SQL
47# execsql2 SQL
48# explain_no_trace SQL
49# explain SQL ?DB?
50# catchsql SQL ?DB?
51# execsql SQL ?DB?
52#
53# Commands to run test cases:
54#
55# do_ioerr_test TESTNAME ARGS...
56# crashsql ARGS...
57# integrity_check TESTNAME ?DB?
drh433dccf2013-02-09 15:37:11 +000058# verify_ex_errcode TESTNAME EXPECTED ?DB?
danc1a60c52010-06-07 14:28:16 +000059# do_test TESTNAME SCRIPT EXPECTED
dan153eda02010-06-21 07:45:47 +000060# do_execsql_test TESTNAME SQL EXPECTED
61# do_catchsql_test TESTNAME SQL EXPECTED
drh3c2aeae2014-01-24 14:37:44 +000062# do_timed_execsql_test TESTNAME SQL EXPECTED
danc1a60c52010-06-07 14:28:16 +000063#
dan430e74c2010-06-07 17:47:26 +000064# Commands providing a lower level interface to the global test counters:
danc1a60c52010-06-07 14:28:16 +000065#
66# set_test_counter COUNTER ?VALUE?
mistachkin6c3c1a02011-11-12 03:17:40 +000067# omit_test TESTNAME REASON ?APPEND?
danc1a60c52010-06-07 14:28:16 +000068# fail_test TESTNAME
69# incr_ntest
70#
71# Command run at the end of each test file:
72#
73# finish_test
74#
dan430e74c2010-06-07 17:47:26 +000075# Commands to help create test files that run with the "WAL" and other
76# permutations (see file permutations.test):
danc1a60c52010-06-07 14:28:16 +000077#
78# wal_is_wal_mode
79# wal_set_journal_mode ?DB?
80# wal_check_journal_mode TESTNAME?DB?
dan430e74c2010-06-07 17:47:26 +000081# permutation
dancb79e512010-08-06 13:50:07 +000082# presql
danc1a60c52010-06-07 14:28:16 +000083#
dan17c08232015-06-09 15:58:28 +000084# Command to test whether or not --verbose=1 was specified on the command
85# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
86# output file only").
87#
88# verbose
89#
drh348784e2000-05-29 20:41:49 +000090
mistachkin1d406e02013-08-29 01:09:14 +000091# Set the precision of FP arithmatic used by the interpreter. And
danc1a60c52010-06-07 14:28:16 +000092# configure SQLite to take database file locks on the page that begins
93# 64KB into the database file instead of the one 1GB in. This means
94# the code that handles that special case can be tested without creating
95# very large database files.
96#
drh92febd92004-08-20 18:34:20 +000097set tcl_precision 15
drhc7a3bb92009-02-05 16:31:45 +000098sqlite3_test_control_pending_byte 0x0010000
drh92febd92004-08-20 18:34:20 +000099
danc1a60c52010-06-07 14:28:16 +0000100
mistachkin1d406e02013-08-29 01:09:14 +0000101# If the pager codec is available, create a wrapper for the [sqlite3]
danc1a60c52010-06-07 14:28:16 +0000102# command that appends "-key {xyzzy}" to the command line. i.e. this:
drh3a7fb7c2007-08-10 16:41:08 +0000103#
danc1a60c52010-06-07 14:28:16 +0000104# sqlite3 db test.db
drh4a50aac2007-08-23 02:47:53 +0000105#
danc1a60c52010-06-07 14:28:16 +0000106# becomes
drh4a50aac2007-08-23 02:47:53 +0000107#
danc1a60c52010-06-07 14:28:16 +0000108# sqlite3 db test.db -key {xyzzy}
drh6a288a32008-01-07 19:20:24 +0000109#
dan430e74c2010-06-07 17:47:26 +0000110if {[info command sqlite_orig]==""} {
drhef4ac8f2004-06-19 00:16:31 +0000111 rename sqlite3 sqlite_orig
112 proc sqlite3 {args} {
dan68928b62010-06-22 13:46:43 +0000113 if {[llength $args]>=2 && [string index [lindex $args 0] 0]!="-"} {
dan430e74c2010-06-07 17:47:26 +0000114 # This command is opening a new database connection.
115 #
116 if {[info exists ::G(perm:sqlite3_args)]} {
117 set args [concat $args $::G(perm:sqlite3_args)]
118 }
dan68928b62010-06-22 13:46:43 +0000119 if {[sqlite_orig -has-codec] && ![info exists ::do_not_use_codec]} {
dan430e74c2010-06-07 17:47:26 +0000120 lappend args -key {xyzzy}
121 }
122
dan3ccd20a2010-06-09 19:01:02 +0000123 set res [uplevel 1 sqlite_orig $args]
dan430e74c2010-06-07 17:47:26 +0000124 if {[info exists ::G(perm:presql)]} {
125 [lindex $args 0] eval $::G(perm:presql)
126 }
dan1ce1b4a2010-12-07 14:32:28 +0000127 if {[info exists ::G(perm:dbconfig)]} {
128 set ::dbhandle [lindex $args 0]
129 uplevel #0 $::G(perm:dbconfig)
130 }
dan3ccd20a2010-06-09 19:01:02 +0000131 set res
dan430e74c2010-06-07 17:47:26 +0000132 } else {
mistachkin1d406e02013-08-29 01:09:14 +0000133 # This command is not opening a new database connection. Pass the
mistachkin48864df2013-03-21 21:20:32 +0000134 # arguments through to the C implementation as the are.
dan430e74c2010-06-07 17:47:26 +0000135 #
136 uplevel 1 sqlite_orig $args
drh9eb9e262004-02-11 02:18:05 +0000137 }
drh9eb9e262004-02-11 02:18:05 +0000138 }
139}
140
mistachkinfda06be2011-08-02 00:57:34 +0000141proc getFileRetries {} {
142 if {![info exists ::G(file-retries)]} {
143 #
144 # NOTE: Return the default number of retries for [file] operations. A
145 # value of zero or less here means "disabled".
146 #
mistachkinc60941f2012-09-13 01:51:02 +0000147 return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
mistachkinfda06be2011-08-02 00:57:34 +0000148 }
149 return $::G(file-retries)
150}
151
152proc getFileRetryDelay {} {
153 if {![info exists ::G(file-retry-delay)]} {
154 #
155 # NOTE: Return the default number of milliseconds to wait when retrying
156 # failed [file] operations. A value of zero or less means "do not
157 # wait".
158 #
159 return 100; # TODO: Good default?
160 }
161 return $::G(file-retry-delay)
162}
163
mistachkinf8a78462012-03-08 20:00:36 +0000164# Return the string representing the name of the current directory. On
165# Windows, the result is "normalized" to whatever our parent command shell
166# is using to prevent case-mismatch issues.
167#
168proc get_pwd {} {
169 if {$::tcl_platform(platform) eq "windows"} {
mistachkin533b8f62012-03-08 20:28:31 +0000170 #
171 # NOTE: Cannot use [file normalize] here because it would alter the
172 # case of the result to what Tcl considers canonical, which would
173 # defeat the purpose of this procedure.
174 #
175 return [string map [list \\ /] \
176 [string trim [exec -- $::env(ComSpec) /c echo %CD%]]]
mistachkinf8a78462012-03-08 20:00:36 +0000177 } else {
178 return [pwd]
179 }
180}
181
mistachkinfda06be2011-08-02 00:57:34 +0000182# Copy file $from into $to. This is used because some versions of
183# TCL for windows (notably the 8.4.1 binary package shipped with the
184# current mingw release) have a broken "file copy" command.
185#
186proc copy_file {from to} {
187 do_copy_file false $from $to
188}
189
190proc forcecopy {from to} {
191 do_copy_file true $from $to
192}
193
194proc do_copy_file {force from to} {
195 set nRetry [getFileRetries] ;# Maximum number of retries.
196 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
197
198 # On windows, sometimes even a [file copy -force] can fail. The cause is
199 # usually "tag-alongs" - programs like anti-virus software, automatic backup
200 # tools and various explorer extensions that keep a file open a little longer
201 # than we expect, causing the delete to fail.
202 #
203 # The solution is to wait a short amount of time before retrying the copy.
204 #
205 if {$nRetry > 0} {
206 for {set i 0} {$i<$nRetry} {incr i} {
207 set rc [catch {
208 if {$force} {
209 file copy -force $from $to
210 } else {
211 file copy $from $to
212 }
213 } msg]
214 if {$rc==0} break
215 if {$nDelay > 0} { after $nDelay }
216 }
217 if {$rc} { error $msg }
218 } else {
219 if {$force} {
220 file copy -force $from $to
221 } else {
222 file copy $from $to
223 }
224 }
225}
226
mistachkinc5484652012-03-05 22:52:33 +0000227# Check if a file name is relative
228#
229proc is_relative_file { file } {
230 return [expr {[file pathtype $file] != "absolute"}]
231}
232
mistachkin4a41f342012-03-06 03:00:49 +0000233# If the VFS supports using the current directory, returns [pwd];
234# otherwise, it returns only the provided suffix string (which is
235# empty by default).
236#
237proc test_pwd { args } {
238 if {[llength $args] > 0} {
239 set suffix1 [lindex $args 0]
240 if {[llength $args] > 1} {
241 set suffix2 [lindex $args 1]
242 } else {
243 set suffix2 $suffix1
244 }
245 } else {
246 set suffix1 ""; set suffix2 ""
247 }
248 ifcapable curdir {
mistachkin6aa18c92012-03-08 20:22:42 +0000249 return "[get_pwd]$suffix1"
mistachkin4a41f342012-03-06 03:00:49 +0000250 } else {
251 return $suffix2
252 }
253}
254
mistachkinfda06be2011-08-02 00:57:34 +0000255# Delete a file or directory
256#
257proc delete_file {args} {
258 do_delete_file false {*}$args
259}
260
261proc forcedelete {args} {
262 do_delete_file true {*}$args
263}
264
265proc do_delete_file {force args} {
266 set nRetry [getFileRetries] ;# Maximum number of retries.
267 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
268
269 foreach filename $args {
270 # On windows, sometimes even a [file delete -force] can fail just after
271 # a file is closed. The cause is usually "tag-alongs" - programs like
272 # anti-virus software, automatic backup tools and various explorer
273 # extensions that keep a file open a little longer than we expect, causing
274 # the delete to fail.
275 #
276 # The solution is to wait a short amount of time before retrying the
277 # delete.
278 #
279 if {$nRetry > 0} {
280 for {set i 0} {$i<$nRetry} {incr i} {
281 set rc [catch {
282 if {$force} {
283 file delete -force $filename
284 } else {
285 file delete $filename
286 }
287 } msg]
288 if {$rc==0} break
289 if {$nDelay > 0} { after $nDelay }
290 }
291 if {$rc} { error $msg }
292 } else {
293 if {$force} {
294 file delete -force $filename
295 } else {
296 file delete $filename
297 }
298 }
299 }
300}
301
mistachkin1d406e02013-08-29 01:09:14 +0000302if {$::tcl_platform(platform) eq "windows"} {
303 proc do_remove_win32_dir {args} {
304 set nRetry [getFileRetries] ;# Maximum number of retries.
305 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
306
307 foreach dirName $args {
308 # On windows, sometimes even a [remove_win32_dir] can fail just after
309 # a directory is emptied. The cause is usually "tag-alongs" - programs
310 # like anti-virus software, automatic backup tools and various explorer
311 # extensions that keep a file open a little longer than we expect,
312 # causing the delete to fail.
313 #
314 # The solution is to wait a short amount of time before retrying the
315 # removal.
316 #
317 if {$nRetry > 0} {
318 for {set i 0} {$i < $nRetry} {incr i} {
319 set rc [catch {
320 remove_win32_dir $dirName
321 } msg]
322 if {$rc == 0} break
323 if {$nDelay > 0} { after $nDelay }
324 }
325 if {$rc} { error $msg }
326 } else {
327 remove_win32_dir $dirName
328 }
329 }
330 }
331
332 proc do_delete_win32_file {args} {
333 set nRetry [getFileRetries] ;# Maximum number of retries.
334 set nDelay [getFileRetryDelay] ;# Delay in ms before retrying.
335
336 foreach fileName $args {
337 # On windows, sometimes even a [delete_win32_file] can fail just after
338 # a file is closed. The cause is usually "tag-alongs" - programs like
339 # anti-virus software, automatic backup tools and various explorer
340 # extensions that keep a file open a little longer than we expect,
341 # causing the delete to fail.
342 #
343 # The solution is to wait a short amount of time before retrying the
344 # delete.
345 #
346 if {$nRetry > 0} {
347 for {set i 0} {$i < $nRetry} {incr i} {
348 set rc [catch {
349 delete_win32_file $fileName
350 } msg]
351 if {$rc == 0} break
352 if {$nDelay > 0} { after $nDelay }
353 }
354 if {$rc} { error $msg }
355 } else {
356 delete_win32_file $fileName
357 }
358 }
359 }
360}
361
dancb354602010-07-08 09:44:42 +0000362proc execpresql {handle args} {
363 trace remove execution $handle enter [list execpresql $handle]
364 if {[info exists ::G(perm:presql)]} {
365 $handle eval $::G(perm:presql)
366 }
367}
drh9eb9e262004-02-11 02:18:05 +0000368
dan68928b62010-06-22 13:46:43 +0000369# This command should be called after loading tester.tcl from within
370# all test scripts that are incompatible with encryption codecs.
drhbec2bf42000-05-29 23:48:22 +0000371#
dan68928b62010-06-22 13:46:43 +0000372proc do_not_use_codec {} {
373 set ::do_not_use_codec 1
374 reset_db
375}
drh422dded2016-07-25 16:10:43 +0000376unset -nocomplain do_not_use_codec
dan68928b62010-06-22 13:46:43 +0000377
drhaf3906a2016-03-14 17:05:04 +0000378# Return true if the "reserved_bytes" integer on database files is non-zero.
379#
380proc nonzero_reserved_bytes {} {
381 return [sqlite3 -has-codec]
382}
383
drh4b7b1c92016-02-15 19:38:17 +0000384# Print a HELP message and exit
385#
386proc print_help_and_quit {} {
387 puts {Options:
388 --pause Wait for user input before continuing
389 --soft-heap-limit=N Set the soft-heap-limit to N
390 --maxerror=N Quit after N errors
391 --verbose=(0|1) Control the amount of output. Default '1'
392 --output=FILE set --verbose=2 and output to FILE. Implies -q
393 -q Shorthand for --verbose=0
394 --help This message
395}
396 exit 1
397}
398
dan430e74c2010-06-07 17:47:26 +0000399# The following block only runs the first time this file is sourced. It
400# does not run in slave interpreters (since the ::cmdlinearg array is
401# populated before the test script is run in slave interpreters).
drhbec2bf42000-05-29 23:48:22 +0000402#
danc1a60c52010-06-07 14:28:16 +0000403if {[info exists cmdlinearg]==0} {
404
mistachkin1d406e02013-08-29 01:09:14 +0000405 # Parse any options specified in the $argv array. This script accepts the
406 # following options:
danc1a60c52010-06-07 14:28:16 +0000407 #
408 # --pause
409 # --soft-heap-limit=NN
410 # --maxerror=NN
411 # --malloctrace=N
412 # --backtrace=N
413 # --binarylog=N
dan0626dfc2010-06-15 06:56:37 +0000414 # --soak=N
mistachkinfda06be2011-08-02 00:57:34 +0000415 # --file-retries=N
416 # --file-retry-delay=N
dan6a64d672011-04-04 15:38:16 +0000417 # --start=[$permutation:]$testfile
mistachkin6c3c1a02011-11-12 03:17:40 +0000418 # --match=$pattern
dan17c08232015-06-09 15:58:28 +0000419 # --verbose=$val
420 # --output=$filename
drhd5704a82016-03-14 13:42:29 +0000421 # -q Reduce output
422 # --testdir=$dir Run tests in subdirectory $dir
dan17c08232015-06-09 15:58:28 +0000423 # --help
danc1a60c52010-06-07 14:28:16 +0000424 #
425 set cmdlinearg(soft-heap-limit) 0
426 set cmdlinearg(maxerror) 1000
427 set cmdlinearg(malloctrace) 0
428 set cmdlinearg(backtrace) 10
429 set cmdlinearg(binarylog) 0
dan0626dfc2010-06-15 06:56:37 +0000430 set cmdlinearg(soak) 0
mistachkinfda06be2011-08-02 00:57:34 +0000431 set cmdlinearg(file-retries) 0
432 set cmdlinearg(file-retry-delay) 0
mistachkin6c3c1a02011-11-12 03:17:40 +0000433 set cmdlinearg(start) ""
434 set cmdlinearg(match) ""
dan17c08232015-06-09 15:58:28 +0000435 set cmdlinearg(verbose) ""
436 set cmdlinearg(output) ""
drhd5704a82016-03-14 13:42:29 +0000437 set cmdlinearg(testdir) "testdir"
danc1a60c52010-06-07 14:28:16 +0000438
439 set leftover [list]
440 foreach a $argv {
441 switch -regexp -- $a {
442 {^-+pause$} {
mistachkin1d406e02013-08-29 01:09:14 +0000443 # Wait for user input before continuing. This is to give the user an
danc1a60c52010-06-07 14:28:16 +0000444 # opportunity to connect profiling tools to the process.
445 puts -nonewline "Press RETURN to begin..."
446 flush stdout
447 gets stdin
448 }
449 {^-+soft-heap-limit=.+$} {
450 foreach {dummy cmdlinearg(soft-heap-limit)} [split $a =] break
451 }
452 {^-+maxerror=.+$} {
453 foreach {dummy cmdlinearg(maxerror)} [split $a =] break
454 }
455 {^-+malloctrace=.+$} {
456 foreach {dummy cmdlinearg(malloctrace)} [split $a =] break
457 if {$cmdlinearg(malloctrace)} {
458 sqlite3_memdebug_log start
459 }
460 }
461 {^-+backtrace=.+$} {
462 foreach {dummy cmdlinearg(backtrace)} [split $a =] break
dan0626dfc2010-06-15 06:56:37 +0000463 sqlite3_memdebug_backtrace $value
danc1a60c52010-06-07 14:28:16 +0000464 }
danc1a60c52010-06-07 14:28:16 +0000465 {^-+binarylog=.+$} {
466 foreach {dummy cmdlinearg(binarylog)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000467 set cmdlinearg(binarylog) [file normalize $cmdlinearg(binarylog)]
danc1a60c52010-06-07 14:28:16 +0000468 }
dan0626dfc2010-06-15 06:56:37 +0000469 {^-+soak=.+$} {
470 foreach {dummy cmdlinearg(soak)} [split $a =] break
471 set ::G(issoak) $cmdlinearg(soak)
472 }
mistachkinfda06be2011-08-02 00:57:34 +0000473 {^-+file-retries=.+$} {
474 foreach {dummy cmdlinearg(file-retries)} [split $a =] break
475 set ::G(file-retries) $cmdlinearg(file-retries)
476 }
477 {^-+file-retry-delay=.+$} {
478 foreach {dummy cmdlinearg(file-retry-delay)} [split $a =] break
479 set ::G(file-retry-delay) $cmdlinearg(file-retry-delay)
480 }
dan6a64d672011-04-04 15:38:16 +0000481 {^-+start=.+$} {
482 foreach {dummy cmdlinearg(start)} [split $a =] break
483
484 set ::G(start:file) $cmdlinearg(start)
485 if {[regexp {(.*):(.*)} $cmdlinearg(start) -> s.perm s.file]} {
486 set ::G(start:permutation) ${s.perm}
487 set ::G(start:file) ${s.file}
488 }
489 if {$::G(start:file) == ""} {unset ::G(start:file)}
490 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000491 {^-+match=.+$} {
492 foreach {dummy cmdlinearg(match)} [split $a =] break
493
494 set ::G(match) $cmdlinearg(match)
495 if {$::G(match) == ""} {unset ::G(match)}
496 }
dan17c08232015-06-09 15:58:28 +0000497
498 {^-+output=.+$} {
499 foreach {dummy cmdlinearg(output)} [split $a =] break
drhe500f652016-03-14 14:59:35 +0000500 set cmdlinearg(output) [file normalize $cmdlinearg(output)]
dan17c08232015-06-09 15:58:28 +0000501 if {$cmdlinearg(verbose)==""} {
502 set cmdlinearg(verbose) 2
503 }
504 }
505 {^-+verbose=.+$} {
506 foreach {dummy cmdlinearg(verbose)} [split $a =] break
507 if {$cmdlinearg(verbose)=="file"} {
508 set cmdlinearg(verbose) 2
509 } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
510 error "option --verbose= must be set to a boolean or to \"file\""
511 }
512 }
drhd5704a82016-03-14 13:42:29 +0000513 {^-+testdir=.*$} {
514 foreach {dummy cmdlinearg(testdir)} [split $a =] break
515 }
drh4b7b1c92016-02-15 19:38:17 +0000516 {.*help.*} {
517 print_help_and_quit
518 }
519 {^-q$} {
520 set cmdlinearg(output) test-out.txt
521 set cmdlinearg(verbose) 2
522 }
dan17c08232015-06-09 15:58:28 +0000523
danc1a60c52010-06-07 14:28:16 +0000524 default {
dan40cf36f2016-04-30 19:23:10 +0000525 if {[file tail $a]==$a} {
526 lappend leftover $a
527 } else {
528 lappend leftover [file normalize $a]
529 }
danc1a60c52010-06-07 14:28:16 +0000530 }
531 }
532 }
drhbea14132016-03-14 14:28:43 +0000533 set testdir [file normalize $testdir]
drhd5704a82016-03-14 13:42:29 +0000534 set cmdlinearg(TESTFIXTURE_HOME) [pwd]
drhaf3906a2016-03-14 17:05:04 +0000535 set cmdlinearg(INFO_SCRIPT) [file normalize [info script]]
drhbea14132016-03-14 14:28:43 +0000536 set argv0 [file normalize $argv0]
drhd5704a82016-03-14 13:42:29 +0000537 if {$cmdlinearg(testdir)!=""} {
538 file mkdir $cmdlinearg(testdir)
539 cd $cmdlinearg(testdir)
540 }
danc1a60c52010-06-07 14:28:16 +0000541 set argv $leftover
542
dan430e74c2010-06-07 17:47:26 +0000543 # Install the malloc layer used to inject OOM errors. And the 'automatic'
544 # extensions. This only needs to be done once for the process.
545 #
mistachkin1d406e02013-08-29 01:09:14 +0000546 sqlite3_shutdown
547 install_malloc_faultsim 1
danielk1977f7b9d662008-06-23 18:49:43 +0000548 sqlite3_initialize
drhbb77b752009-04-09 01:23:49 +0000549 autoinstall_test_functions
danc1a60c52010-06-07 14:28:16 +0000550
dan430e74c2010-06-07 17:47:26 +0000551 # If the --binarylog option was specified, create the logging VFS. This
552 # call installs the new VFS as the default for all SQLite connections.
553 #
danc1a60c52010-06-07 14:28:16 +0000554 if {$cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +0000555 vfslog new binarylog {} vfslog.bin
danc1a60c52010-06-07 14:28:16 +0000556 }
557
558 # Set the backtrace depth, if malloc tracing is enabled.
559 #
560 if {$cmdlinearg(malloctrace)} {
561 sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
danielk1977185eac92008-07-12 15:55:54 +0000562 }
dan17c08232015-06-09 15:58:28 +0000563
564 if {$cmdlinearg(output)!=""} {
565 puts "Copying output to file $cmdlinearg(output)"
566 set ::G(output_fd) [open $cmdlinearg(output) w]
567 fconfigure $::G(output_fd) -buffering line
568 }
569
570 if {$cmdlinearg(verbose)==""} {
571 set cmdlinearg(verbose) 1
572 }
danielk1977f7b9d662008-06-23 18:49:43 +0000573}
danielk197703ba3fa2009-01-09 10:49:14 +0000574
danc1a60c52010-06-07 14:28:16 +0000575# Update the soft-heap-limit each time this script is run. In that
576# way if an individual test file changes the soft-heap-limit, it
577# will be reset at the start of the next test file.
578#
579sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
580
adam2e4491d2011-06-24 20:47:06 +0000581proc forced_proxy_locking {} {
582 if $::sqlite_options(lock_proxy_pragmas)&&$::sqlite_options(prefer_proxy_locking) {
583 set force_proxy_value 0
584 set force_key "SQLITE_FORCE_PROXY_LOCKING="
585 foreach {env_pair} [exec env] {
586 if { [string first $force_key $env_pair] == 0} {
587 set force_proxy_value [string range $env_pair [string length $force_key] end]
588 }
589 }
590 if { "$force_proxy_value " == "1 " } {
591 return 1
592 }
593 }
594 return 0
595}
596
597
danc1a60c52010-06-07 14:28:16 +0000598# Create a test database
599#
danielk197703ba3fa2009-01-09 10:49:14 +0000600proc reset_db {} {
601 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +0000602 forcedelete test.db
603 forcedelete test.db-journal
604 forcedelete test.db-wal
adam2e4491d2011-06-24 20:47:06 +0000605 if {[forced_proxy_locking]} {
606 sqlite3 db ./test.db
607 set lock_proxy_path [db eval "PRAGMA lock_proxy_file;"]
608 catch {db close}
609 # puts "deleting $lock_proxy_path"
610 file delete -force $lock_proxy_path
611 file delete -force test.db
612 }
danielk197703ba3fa2009-01-09 10:49:14 +0000613 sqlite3 db ./test.db
614 set ::DB [sqlite3_connection_pointer db]
615 if {[info exists ::SETUP_SQL]} {
616 db eval $::SETUP_SQL
617 }
drhcd61c282002-03-06 22:01:34 +0000618}
danielk197703ba3fa2009-01-09 10:49:14 +0000619reset_db
drhbec2bf42000-05-29 23:48:22 +0000620
621# Abort early if this script has been run before.
622#
danc1a60c52010-06-07 14:28:16 +0000623if {[info exists TC(count)]} return
drhbec2bf42000-05-29 23:48:22 +0000624
drh5ec0cfe2010-09-10 00:18:12 +0000625# Make sure memory statistics are enabled.
626#
627sqlite3_config_memstatus 1
628
danc1a60c52010-06-07 14:28:16 +0000629# Initialize the test counters and set up commands to access them.
630# Or, if this is a slave interpreter, set up aliases to write the
631# counters in the parent interpreter.
drhbec2bf42000-05-29 23:48:22 +0000632#
danc1a60c52010-06-07 14:28:16 +0000633if {0==[info exists ::SLAVE]} {
634 set TC(errors) 0
635 set TC(count) 0
636 set TC(fail_list) [list]
637 set TC(omit_list) [list]
drhcca17c32013-04-19 12:32:52 +0000638 set TC(warn_list) [list]
danc1a60c52010-06-07 14:28:16 +0000639
640 proc set_test_counter {counter args} {
641 if {[llength $args]} {
642 set ::TC($counter) [lindex $args 0]
643 }
644 set ::TC($counter)
645 }
drhb62c3352006-11-23 09:39:16 +0000646}
drh348784e2000-05-29 20:41:49 +0000647
drh521cc842008-04-15 02:36:33 +0000648# Record the fact that a sequence of tests were omitted.
649#
mistachkin6c3c1a02011-11-12 03:17:40 +0000650proc omit_test {name reason {append 1}} {
danc1a60c52010-06-07 14:28:16 +0000651 set omitList [set_test_counter omit_list]
mistachkin6c3c1a02011-11-12 03:17:40 +0000652 if {$append} {
653 lappend omitList [list $name $reason]
654 }
danc1a60c52010-06-07 14:28:16 +0000655 set_test_counter omit_list $omitList
drh521cc842008-04-15 02:36:33 +0000656}
657
danc1a60c52010-06-07 14:28:16 +0000658# Record the fact that a test failed.
659#
660proc fail_test {name} {
661 set f [set_test_counter fail_list]
662 lappend f $name
663 set_test_counter fail_list $f
664 set_test_counter errors [expr [set_test_counter errors] + 1]
665
666 set nFail [set_test_counter errors]
667 if {$nFail>=$::cmdlinearg(maxerror)} {
dan17c08232015-06-09 15:58:28 +0000668 output2 "*** Giving up..."
danc1a60c52010-06-07 14:28:16 +0000669 finalize_testing
670 }
671}
672
drhcca17c32013-04-19 12:32:52 +0000673# Remember a warning message to be displayed at the conclusion of all testing
674#
675proc warning {msg {append 1}} {
dan17c08232015-06-09 15:58:28 +0000676 output2 "Warning: $msg"
drhcca17c32013-04-19 12:32:52 +0000677 set warnList [set_test_counter warn_list]
678 if {$append} {
679 lappend warnList $msg
680 }
681 set_test_counter warn_list $warnList
682}
683
684
danc1a60c52010-06-07 14:28:16 +0000685# Increment the number of tests run
686#
687proc incr_ntest {} {
688 set_test_counter count [expr [set_test_counter count] + 1]
689}
690
dan17c08232015-06-09 15:58:28 +0000691# Return true if --verbose=1 was specified on the command line. Otherwise,
692# return false.
693#
694proc verbose {} {
695 return $::cmdlinearg(verbose)
696}
697
698# Use the following commands instead of [puts] for test output within
699# this file. Test scripts can still use regular [puts], which is directed
700# to stdout and, if one is open, the --output file.
701#
702# output1: output that should be printed if --verbose=1 was specified.
703# output2: output that should be printed unconditionally.
704# output2_if_no_verbose: output that should be printed only if --verbose=0.
705#
706proc output1 {args} {
707 set v [verbose]
708 if {$v==1} {
709 uplevel output2 $args
710 } elseif {$v==2} {
711 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
712 }
713}
714proc output2 {args} {
715 set nArg [llength $args]
716 uplevel puts $args
717}
718proc output2_if_no_verbose {args} {
719 set v [verbose]
720 if {$v==0} {
721 uplevel output2 $args
722 } elseif {$v==2} {
723 uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
724 }
725}
726
727# Override the [puts] command so that if no channel is explicitly
728# specified the string is written to both stdout and to the file
729# specified by "--output=", if any.
730#
731proc puts_override {args} {
732 set nArg [llength $args]
733 if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
734 uplevel puts_original $args
735 if {[info exists ::G(output_fd)]} {
736 uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
737 }
738 } else {
739 # A channel was explicitly specified.
740 uplevel puts_original $args
741 }
742}
743rename puts puts_original
744proc puts {args} { uplevel puts_override $args }
745
danc1a60c52010-06-07 14:28:16 +0000746
mistachkin1d406e02013-08-29 01:09:14 +0000747# Invoke the do_test procedure to run a single test
drh348784e2000-05-29 20:41:49 +0000748#
drhe39cd912016-07-09 17:47:01 +0000749# The $expected parameter is the expected result. The result is the return
750# value from the last TCL command in $cmd.
751#
752# Normally, $expected must match exactly. But if $expected is of the form
753# "/regexp/" then regular expression matching is used. If $expected is
754# "~/regexp/" then the regular expression must NOT match. If $expected is
755# of the form "#/value-list/" then each term in value-list must be numeric
756# and must approximately match the corresponding numeric term in $result.
757# Values must match within 10%. Or if the $expected term is A..B then the
758# $result term must be in between A and B.
759#
drh348784e2000-05-29 20:41:49 +0000760proc do_test {name cmd expected} {
danc1a60c52010-06-07 14:28:16 +0000761 global argv cmdlinearg
762
dan8c408002010-11-01 17:38:24 +0000763 fix_testname name
764
drh4a50aac2007-08-23 02:47:53 +0000765 sqlite3_memdebug_settitle $name
danc1a60c52010-06-07 14:28:16 +0000766
mistachkin1d406e02013-08-29 01:09:14 +0000767# if {[llength $argv]==0} {
dan92d516a2010-07-05 14:54:48 +0000768# set go 1
769# } else {
770# set go 0
771# foreach pattern $argv {
772# if {[string match $pattern $name]} {
773# set go 1
774# break
775# }
776# }
777# }
dan430e74c2010-06-07 17:47:26 +0000778
dand506de02010-07-03 13:59:01 +0000779 if {[info exists ::G(perm:prefix)]} {
780 set name "$::G(perm:prefix)$name"
danielk1977374177e2008-05-08 15:58:06 +0000781 }
dan430e74c2010-06-07 17:47:26 +0000782
danc1a60c52010-06-07 14:28:16 +0000783 incr_ntest
dan17c08232015-06-09 15:58:28 +0000784 output1 -nonewline $name...
drh348784e2000-05-29 20:41:49 +0000785 flush stdout
mistachkin6c3c1a02011-11-12 03:17:40 +0000786
787 if {![info exists ::G(match)] || [string match $::G(match) $name]} {
788 if {[catch {uplevel #0 "$cmd;\n"} result]} {
dan17c08232015-06-09 15:58:28 +0000789 output2_if_no_verbose -nonewline $name...
790 output2 "\nError: $result"
mistachkin6c3c1a02011-11-12 03:17:40 +0000791 fail_test $name
mistachkin6c3c1a02011-11-12 03:17:40 +0000792 } else {
drhe39cd912016-07-09 17:47:01 +0000793 if {[regexp {^[~#]?/.*/$} $expected]} {
drh70bdcc72013-05-30 19:28:34 +0000794 # "expected" is of the form "/PATTERN/" then the result if correct if
795 # regular expression PATTERN matches the result. "~/PATTERN/" means
796 # the regular expression must not match.
drh3f17aef2012-04-27 01:08:02 +0000797 if {[string index $expected 0]=="~"} {
drhc2b23e72013-11-13 15:32:15 +0000798 set re [string range $expected 2 end-1]
799 if {[string index $re 0]=="*"} {
800 # If the regular expression begins with * then treat it as a glob instead
801 set ok [string match $re $result]
802 } else {
803 set re [string map {# {[-0-9.]+}} $re]
804 set ok [regexp $re $result]
805 }
806 set ok [expr {!$ok}]
drhe39cd912016-07-09 17:47:01 +0000807 } elseif {[string index $expected 0]=="#"} {
808 # Numeric range value comparison. Each term of the $result is matched
809 # against one term of $expect. Both $result and $expected terms must be
810 # numeric. The values must match within 10%. Or if $expected is of the
811 # form A..B then the $result term must be between A and B.
812 set e2 [string range $expected 2 end-1]
813 foreach i $result j $e2 {
814 if {[regexp {^(-?\d+)\.\.(-?\d)$} $j all A B]} {
815 set ok [expr {$i+0>=$A && $i+0<=$B}]
816 } else {
817 set ok [expr {$i+0>=0.9*$j && $i+0<=1.1*$j}]
818 }
819 if {!$ok} break
820 }
821 if {$ok && [llength $result]!=[llength $e2]} {set ok 0}
drh3f17aef2012-04-27 01:08:02 +0000822 } else {
drhc2b23e72013-11-13 15:32:15 +0000823 set re [string range $expected 1 end-1]
824 if {[string index $re 0]=="*"} {
825 # If the regular expression begins with * then treat it as a glob instead
826 set ok [string match $re $result]
827 } else {
828 set re [string map {# {[-0-9.]+}} $re]
829 set ok [regexp $re $result]
830 }
drh3f17aef2012-04-27 01:08:02 +0000831 }
drh70bdcc72013-05-30 19:28:34 +0000832 } elseif {[regexp {^~?\*.*\*$} $expected]} {
833 # "expected" is of the form "*GLOB*" then the result if correct if
834 # glob pattern GLOB matches the result. "~/GLOB/" means
835 # the glob must not match.
836 if {[string index $expected 0]=="~"} {
837 set e [string range $expected 1 end]
838 set ok [expr {![string match $e $result]}]
839 } else {
840 set ok [string match $expected $result]
drh3f17aef2012-04-27 01:08:02 +0000841 }
842 } else {
843 set ok [expr {[string compare $result $expected]==0}]
844 }
845 if {!$ok} {
mistachkinc60941f2012-09-13 01:51:02 +0000846 # if {![info exists ::testprefix] || $::testprefix eq ""} {
847 # error "no test prefix"
848 # }
drhd66b2e02015-11-12 21:42:40 +0000849 output1 ""
drh061d35c2015-11-13 00:03:14 +0000850 output2 "! $name expected: \[$expected\]\n! $name got: \[$result\]"
drh3f17aef2012-04-27 01:08:02 +0000851 fail_test $name
852 } else {
dan17c08232015-06-09 15:58:28 +0000853 output1 " Ok"
drh3f17aef2012-04-27 01:08:02 +0000854 }
mistachkin6c3c1a02011-11-12 03:17:40 +0000855 }
drh348784e2000-05-29 20:41:49 +0000856 } else {
dan17c08232015-06-09 15:58:28 +0000857 output1 " Omitted"
mistachkin6c3c1a02011-11-12 03:17:40 +0000858 omit_test $name "pattern mismatch" 0
drh348784e2000-05-29 20:41:49 +0000859 }
drh6558db82007-04-13 03:23:21 +0000860 flush stdout
drh348784e2000-05-29 20:41:49 +0000861}
dana3f51082010-09-30 18:43:14 +0000862
mistachkinf21979d2015-01-18 05:35:01 +0000863proc dumpbytes {s} {
864 set r ""
865 for {set i 0} {$i < [string length $s]} {incr i} {
866 if {$i > 0} {append r " "}
867 append r [format %02X [scan [string index $s $i] %c]]
868 }
869 return $r
870}
871
drh8df91852012-04-24 12:46:05 +0000872proc catchcmd {db {cmd ""}} {
873 global CLI
874 set out [open cmds.txt w]
875 puts $out $cmd
876 close $out
877 set line "exec $CLI $db < cmds.txt"
878 set rc [catch { eval $line } msg]
879 list $rc $msg
880}
881
mistachkinf21979d2015-01-18 05:35:01 +0000882proc catchcmdex {db {cmd ""}} {
883 global CLI
884 set out [open cmds.txt w]
885 fconfigure $out -encoding binary -translation binary
886 puts -nonewline $out $cmd
887 close $out
888 set line "exec -keepnewline -- $CLI $db < cmds.txt"
889 set chans [list stdin stdout stderr]
890 foreach chan $chans {
891 catch {
892 set modes($chan) [fconfigure $chan]
893 fconfigure $chan -encoding binary -translation binary -buffering none
894 }
895 }
896 set rc [catch { eval $line } msg]
897 foreach chan $chans {
898 catch {
899 eval fconfigure [list $chan] $modes($chan)
900 }
901 }
902 # puts [dumpbytes $msg]
903 list $rc $msg
904}
905
shaneh55505172011-06-21 19:30:19 +0000906proc filepath_normalize {p} {
907 # test cases should be written to assume "unix"-like file paths
shaneh285a18f2011-06-21 19:39:59 +0000908 if {$::tcl_platform(platform)!="unix"} {
shaneh55505172011-06-21 19:30:19 +0000909 # lreverse*2 as a hack to remove any unneeded {} after the string map
910 lreverse [lreverse [string map {\\ /} [regsub -nocase -all {[a-z]:[/\\]+} $p {/}]]]
shanehc4896402011-06-21 19:38:16 +0000911 } {
912 set p
shaneh55505172011-06-21 19:30:19 +0000913 }
914}
915proc do_filepath_test {name cmd expected} {
916 uplevel [list do_test $name [
917 subst -nocommands { filepath_normalize [ $cmd ] }
918 ] [filepath_normalize $expected]]
919}
920
dan33f53792011-05-05 19:44:22 +0000921proc realnum_normalize {r} {
shaneh4f529e82011-06-20 17:41:41 +0000922 # different TCL versions display floating point values differently.
923 string map {1.#INF inf Inf inf .0e e} [regsub -all {(e[+-])0+} $r {\1}]
dan33f53792011-05-05 19:44:22 +0000924}
925proc do_realnum_test {name cmd expected} {
926 uplevel [list do_test $name [
927 subst -nocommands { realnum_normalize [ $cmd ] }
928 ] [realnum_normalize $expected]]
929}
930
dana3f51082010-09-30 18:43:14 +0000931proc fix_testname {varname} {
932 upvar $varname testname
mistachkin1d406e02013-08-29 01:09:14 +0000933 if {[info exists ::testprefix]
dana3f51082010-09-30 18:43:14 +0000934 && [string is digit [string range $testname 0 0]]
935 } {
936 set testname "${::testprefix}-$testname"
937 }
938}
mistachkin1d406e02013-08-29 01:09:14 +0000939
dan67340072011-04-16 19:23:10 +0000940proc normalize_list {L} {
941 set L2 [list]
942 foreach l $L {lappend L2 $l}
943 set L2
944}
drh5f9742e2013-08-29 15:08:38 +0000945
dan57f7f4b2010-10-01 19:04:37 +0000946proc do_execsql_test {testname sql {result {}}} {
dana3f51082010-09-30 18:43:14 +0000947 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000948 uplevel do_test [list $testname] [list "execsql {$sql}"] [list [list {*}$result]]
dan153eda02010-06-21 07:45:47 +0000949}
950proc do_catchsql_test {testname sql result} {
dana3f51082010-09-30 18:43:14 +0000951 fix_testname testname
dan99ebad92011-06-13 09:11:01 +0000952 uplevel do_test [list $testname] [list "catchsql {$sql}"] [list $result]
dan153eda02010-06-21 07:45:47 +0000953}
drh3c2aeae2014-01-24 14:37:44 +0000954proc do_timed_execsql_test {testname sql {result {}}} {
955 fix_testname testname
956 uplevel do_test [list $testname] [list "execsql_timed {$sql}"]\
957 [list [list {*}$result]]
958}
dan39854792010-11-15 16:12:58 +0000959proc do_eqp_test {name sql res} {
960 uplevel do_execsql_test $name [list "EXPLAIN QUERY PLAN $sql"] [list $res]
961}
dan153eda02010-06-21 07:45:47 +0000962
dan51f06982010-09-18 19:00:12 +0000963#-------------------------------------------------------------------------
964# Usage: do_select_tests PREFIX ?SWITCHES? TESTLIST
965#
966# Where switches are:
967#
968# -errorformat FMTSTRING
969# -count
danaf1dcab2010-09-20 19:17:53 +0000970# -query SQL
dana16d1062010-09-28 17:37:28 +0000971# -tclquery TCL
danaf7626f2010-09-23 18:47:36 +0000972# -repair TCL
dan51f06982010-09-18 19:00:12 +0000973#
974proc do_select_tests {prefix args} {
975
976 set testlist [lindex $args end]
977 set switches [lrange $args 0 end-1]
978
979 set errfmt ""
980 set countonly 0
dana16d1062010-09-28 17:37:28 +0000981 set tclquery ""
danaf7626f2010-09-23 18:47:36 +0000982 set repair ""
dan51f06982010-09-18 19:00:12 +0000983
984 for {set i 0} {$i < [llength $switches]} {incr i} {
985 set s [lindex $switches $i]
986 set n [string length $s]
danaf1dcab2010-09-20 19:17:53 +0000987 if {$n>=2 && [string equal -length $n $s "-query"]} {
dana16d1062010-09-28 17:37:28 +0000988 set tclquery [list execsql [lindex $switches [incr i]]]
989 } elseif {$n>=2 && [string equal -length $n $s "-tclquery"]} {
990 set tclquery [lindex $switches [incr i]]
danaf1dcab2010-09-20 19:17:53 +0000991 } elseif {$n>=2 && [string equal -length $n $s "-errorformat"]} {
dan51f06982010-09-18 19:00:12 +0000992 set errfmt [lindex $switches [incr i]]
danaf7626f2010-09-23 18:47:36 +0000993 } elseif {$n>=2 && [string equal -length $n $s "-repair"]} {
994 set repair [lindex $switches [incr i]]
dan51f06982010-09-18 19:00:12 +0000995 } elseif {$n>=2 && [string equal -length $n $s "-count"]} {
996 set countonly 1
997 } else {
998 error "unknown switch: $s"
999 }
1000 }
1001
1002 if {$countonly && $errfmt!=""} {
1003 error "Cannot use -count and -errorformat together"
1004 }
1005 set nTestlist [llength $testlist]
1006 if {$nTestlist%3 || $nTestlist==0 } {
1007 error "SELECT test list contains [llength $testlist] elements"
1008 }
1009
danaf7626f2010-09-23 18:47:36 +00001010 eval $repair
dan51f06982010-09-18 19:00:12 +00001011 foreach {tn sql res} $testlist {
dana16d1062010-09-28 17:37:28 +00001012 if {$tclquery != ""} {
danaf1dcab2010-09-20 19:17:53 +00001013 execsql $sql
dana16d1062010-09-28 17:37:28 +00001014 uplevel do_test ${prefix}.$tn [list $tclquery] [list [list {*}$res]]
1015 } elseif {$countonly} {
dan51f06982010-09-18 19:00:12 +00001016 set nRow 0
1017 db eval $sql {incr nRow}
1018 uplevel do_test ${prefix}.$tn [list [list set {} $nRow]] [list $res]
1019 } elseif {$errfmt==""} {
1020 uplevel do_execsql_test ${prefix}.${tn} [list $sql] [list [list {*}$res]]
1021 } else {
1022 set res [list 1 [string trim [format $errfmt {*}$res]]]
1023 uplevel do_catchsql_test ${prefix}.${tn} [list $sql] [list $res]
1024 }
danaf7626f2010-09-23 18:47:36 +00001025 eval $repair
dan51f06982010-09-18 19:00:12 +00001026 }
danaf7626f2010-09-23 18:47:36 +00001027
dan51f06982010-09-18 19:00:12 +00001028}
1029
danb04757a2010-09-21 16:59:16 +00001030proc delete_all_data {} {
1031 db eval {SELECT tbl_name AS t FROM sqlite_master WHERE type = 'table'} {
1032 db eval "DELETE FROM '[string map {' ''} $t]'"
1033 }
1034}
drh348784e2000-05-29 20:41:49 +00001035
mistachkin1d406e02013-08-29 01:09:14 +00001036# Run an SQL script.
drhb62c3352006-11-23 09:39:16 +00001037# Return the number of microseconds per statement.
1038#
drh3590f152006-11-23 21:09:10 +00001039proc speed_trial {name numstmt units sql} {
dan17c08232015-06-09 15:58:28 +00001040 output2 -nonewline [format {%-21.21s } $name...]
drhb62c3352006-11-23 09:39:16 +00001041 flush stdout
1042 set speed [time {sqlite3_exec_nr db $sql}]
1043 set tm [lindex $speed 0]
danielk19770ee26d92008-02-08 18:25:29 +00001044 if {$tm == 0} {
1045 set rate [format %20s "many"]
1046 } else {
1047 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1048 }
drh3590f152006-11-23 21:09:10 +00001049 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001050 output2 [format {%12d uS %s %s} $tm $rate $u2]
drhdd924312007-03-31 22:34:16 +00001051 global total_time
1052 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001053 lappend ::speed_trial_times $name $tm
drhdd924312007-03-31 22:34:16 +00001054}
drh45c236d2008-03-22 01:08:00 +00001055proc speed_trial_tcl {name numstmt units script} {
dan17c08232015-06-09 15:58:28 +00001056 output2 -nonewline [format {%-21.21s } $name...]
drh45c236d2008-03-22 01:08:00 +00001057 flush stdout
1058 set speed [time {eval $script}]
1059 set tm [lindex $speed 0]
1060 if {$tm == 0} {
1061 set rate [format %20s "many"]
1062 } else {
1063 set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
1064 }
1065 set u2 $units/s
dan17c08232015-06-09 15:58:28 +00001066 output2 [format {%12d uS %s %s} $tm $rate $u2]
drh45c236d2008-03-22 01:08:00 +00001067 global total_time
1068 set total_time [expr {$total_time+$tm}]
dana975b592010-10-08 16:09:43 +00001069 lappend ::speed_trial_times $name $tm
drh45c236d2008-03-22 01:08:00 +00001070}
drhdd924312007-03-31 22:34:16 +00001071proc speed_trial_init {name} {
1072 global total_time
1073 set total_time 0
dana975b592010-10-08 16:09:43 +00001074 set ::speed_trial_times [list]
drhbb810a92010-07-03 12:00:53 +00001075 sqlite3 versdb :memory:
1076 set vers [versdb one {SELECT sqlite_source_id()}]
1077 versdb close
dan17c08232015-06-09 15:58:28 +00001078 output2 "SQLite $vers"
drhdd924312007-03-31 22:34:16 +00001079}
1080proc speed_trial_summary {name} {
1081 global total_time
dan17c08232015-06-09 15:58:28 +00001082 output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
dana975b592010-10-08 16:09:43 +00001083
1084 if { 0 } {
1085 sqlite3 versdb :memory:
1086 set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
1087 versdb close
dan17c08232015-06-09 15:58:28 +00001088 output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
dana975b592010-10-08 16:09:43 +00001089 foreach {test us} $::speed_trial_times {
dan17c08232015-06-09 15:58:28 +00001090 output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
dana975b592010-10-08 16:09:43 +00001091 }
1092 }
drhb62c3352006-11-23 09:39:16 +00001093}
1094
drh348784e2000-05-29 20:41:49 +00001095# Run this routine last
1096#
1097proc finish_test {} {
danc1a60c52010-06-07 14:28:16 +00001098 catch {db close}
dane8559832014-07-31 17:35:40 +00001099 catch {db1 close}
danc1a60c52010-06-07 14:28:16 +00001100 catch {db2 close}
1101 catch {db3 close}
1102 if {0==[info exists ::SLAVE]} { finalize_testing }
drha1b351a2001-09-14 16:42:12 +00001103}
1104proc finalize_testing {} {
danc1a60c52010-06-07 14:28:16 +00001105 global sqlite_open_file_count
1106
1107 set omitList [set_test_counter omit_list]
danielk19772e588c72005-12-09 14:25:08 +00001108
drh6e142f52000-06-08 13:36:40 +00001109 catch {db close}
danielk19772e588c72005-12-09 14:25:08 +00001110 catch {db2 close}
1111 catch {db3 close}
1112
drh9bc54492007-10-23 14:49:59 +00001113 vfs_unlink_test
drhb4bc7052006-01-11 23:40:33 +00001114 sqlite3 db {}
danielk1977cbb84962006-01-17 16:10:13 +00001115 # sqlite3_clear_tsd_memdebug
drhb4bc7052006-01-11 23:40:33 +00001116 db close
drh984bfaa2008-03-19 16:08:53 +00001117 sqlite3_reset_auto_extension
danc1a60c52010-06-07 14:28:16 +00001118
drh3a7fb7c2007-08-10 16:41:08 +00001119 sqlite3_soft_heap_limit 0
danc1a60c52010-06-07 14:28:16 +00001120 set nTest [incr_ntest]
1121 set nErr [set_test_counter errors]
1122
drhef866372013-05-22 20:49:02 +00001123 set nKnown 0
1124 if {[file readable known-problems.txt]} {
1125 set fd [open known-problems.txt]
1126 set content [read $fd]
1127 close $fd
1128 foreach x $content {set known_error($x) 1}
1129 foreach x [set_test_counter fail_list] {
1130 if {[info exists known_error($x)]} {incr nKnown}
1131 }
1132 }
1133 if {$nKnown>0} {
dan17c08232015-06-09 15:58:28 +00001134 output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
drhef866372013-05-22 20:49:02 +00001135 out of $nTest tests"
1136 } else {
drh72bf6a32016-01-07 02:06:55 +00001137 set cpuinfo {}
1138 if {[catch {exec hostname} hname]==0} {set cpuinfo [string trim $hname]}
1139 append cpuinfo " $::tcl_platform(os)"
1140 append cpuinfo " [expr {$::tcl_platform(pointerSize)*8}]-bit"
1141 append cpuinfo " [string map {E -e} $::tcl_platform(byteOrder)]"
1142 output2 "SQLite [sqlite3 -sourceid]"
1143 output2 "$nErr errors out of $nTest tests on $cpuinfo"
drhef866372013-05-22 20:49:02 +00001144 }
1145 if {$nErr>$nKnown} {
drh061d35c2015-11-13 00:03:14 +00001146 output2 -nonewline "!Failures on these tests:"
drhef866372013-05-22 20:49:02 +00001147 foreach x [set_test_counter fail_list] {
dan17c08232015-06-09 15:58:28 +00001148 if {![info exists known_error($x)]} {output2 -nonewline " $x"}
drhef866372013-05-22 20:49:02 +00001149 }
dan17c08232015-06-09 15:58:28 +00001150 output2 ""
drhf3a65f72007-08-22 20:18:21 +00001151 }
drhcca17c32013-04-19 12:32:52 +00001152 foreach warning [set_test_counter warn_list] {
dan17c08232015-06-09 15:58:28 +00001153 output2 "Warning: $warning"
drha1b351a2001-09-14 16:42:12 +00001154 }
danielk1977ee8b7992009-03-26 17:13:06 +00001155 run_thread_tests 1
drh521cc842008-04-15 02:36:33 +00001156 if {[llength $omitList]>0} {
dan17c08232015-06-09 15:58:28 +00001157 output2 "Omitted test cases:"
drh521cc842008-04-15 02:36:33 +00001158 set prec {}
1159 foreach {rec} [lsort $omitList] {
1160 if {$rec==$prec} continue
1161 set prec $rec
drhd66b2e02015-11-12 21:42:40 +00001162 output2 [format {. %-12s %s} [lindex $rec 0] [lindex $rec 1]]
drh521cc842008-04-15 02:36:33 +00001163 }
1164 }
drh80788d82006-09-02 14:50:23 +00001165 if {$nErr>0 && ![working_64bit_int]} {
dan17c08232015-06-09 15:58:28 +00001166 output2 "******************************************************************"
1167 output2 "N.B.: The version of TCL that you used to build this test harness"
1168 output2 "is defective in that it does not support 64-bit integers. Some or"
1169 output2 "all of the test failures above might be a result from this defect"
1170 output2 "in your TCL build."
1171 output2 "******************************************************************"
drhdb25e382001-03-15 18:21:22 +00001172 }
danc1a60c52010-06-07 14:28:16 +00001173 if {$::cmdlinearg(binarylog)} {
danfbefb892010-05-12 19:02:35 +00001174 vfslog finalize binarylog
danielk1977374177e2008-05-08 15:58:06 +00001175 }
drh94e92032003-02-16 22:21:32 +00001176 if {$sqlite_open_file_count} {
dan17c08232015-06-09 15:58:28 +00001177 output2 "$sqlite_open_file_count files were left open"
drh94e92032003-02-16 22:21:32 +00001178 incr nErr
1179 }
drheafc43b2010-07-26 18:43:40 +00001180 if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 ||
1181 [sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001182 output2 "Unfreed memory: [sqlite3_memory_used] bytes in\
drheafc43b2010-07-26 18:43:40 +00001183 [lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1] allocations"
drhf3a65f72007-08-22 20:18:21 +00001184 incr nErr
drh2d7636e2008-02-16 16:21:45 +00001185 ifcapable memdebug||mem5||(mem3&&debug) {
dan17c08232015-06-09 15:58:28 +00001186 output2 "Writing unfreed memory log to \"./memleak.txt\""
drh4a50aac2007-08-23 02:47:53 +00001187 sqlite3_memdebug_dump ./memleak.txt
1188 }
drhf3a65f72007-08-22 20:18:21 +00001189 } else {
dan17c08232015-06-09 15:58:28 +00001190 output2 "All memory allocations freed - no leaks"
drh2d7636e2008-02-16 16:21:45 +00001191 ifcapable memdebug||mem5 {
drhd2bb3272007-10-15 19:34:32 +00001192 sqlite3_memdebug_dump ./memusage.txt
1193 }
drhf3a65f72007-08-22 20:18:21 +00001194 }
drhf7141992008-06-19 00:16:08 +00001195 show_memstats
dan17c08232015-06-09 15:58:28 +00001196 output2 "Maximum memory usage: [sqlite3_memory_highwater 1] bytes"
1197 output2 "Current memory usage: [sqlite3_memory_highwater] bytes"
danielk1977a7a8e142008-02-13 18:25:27 +00001198 if {[info commands sqlite3_memdebug_malloc_count] ne ""} {
dan17c08232015-06-09 15:58:28 +00001199 output2 "Number of malloc() : [sqlite3_memdebug_malloc_count] calls"
danielk1977a7a8e142008-02-13 18:25:27 +00001200 }
danc1a60c52010-06-07 14:28:16 +00001201 if {$::cmdlinearg(malloctrace)} {
dan17c08232015-06-09 15:58:28 +00001202 output2 "Writing mallocs.sql..."
danielk197735754ac2008-03-21 17:29:37 +00001203 memdebug_log_sql
1204 sqlite3_memdebug_log stop
1205 sqlite3_memdebug_log clear
danielk1977dbdc4d42008-03-28 07:42:53 +00001206
1207 if {[sqlite3_memory_used]>0} {
dan17c08232015-06-09 15:58:28 +00001208 output2 "Writing leaks.sql..."
danielk1977dbdc4d42008-03-28 07:42:53 +00001209 sqlite3_memdebug_log sync
1210 memdebug_log_sql leaks.sql
1211 }
danielk197735754ac2008-03-21 17:29:37 +00001212 }
adame6c3c722009-11-03 22:34:35 +00001213 catch {
1214 foreach f [glob -nocomplain test.db-*-journal] {
dan343811c2011-08-23 18:06:04 +00001215 forcedelete $f
adame6c3c722009-11-03 22:34:35 +00001216 }
drhd9910fe2006-10-04 11:55:49 +00001217 }
adame6c3c722009-11-03 22:34:35 +00001218 catch {
1219 foreach f [glob -nocomplain test.db-mj*] {
dan343811c2011-08-23 18:06:04 +00001220 forcedelete $f
adame6c3c722009-11-03 22:34:35 +00001221 }
drhd9910fe2006-10-04 11:55:49 +00001222 }
drhdb25e382001-03-15 18:21:22 +00001223 exit [expr {$nErr>0}]
drh348784e2000-05-29 20:41:49 +00001224}
1225
drhf7141992008-06-19 00:16:08 +00001226# Display memory statistics for analysis and debugging purposes.
1227#
1228proc show_memstats {} {
1229 set x [sqlite3_status SQLITE_STATUS_MEMORY_USED 0]
drhe50135e2008-08-05 17:53:22 +00001230 set y [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0]
1231 set val [format {now %10d max %10d max-size %10d} \
1232 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001233 output1 "Memory used: $val"
drheafc43b2010-07-26 18:43:40 +00001234 set x [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0]
1235 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001236 output1 "Allocation count: $val"
drhf7141992008-06-19 00:16:08 +00001237 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0]
drhe50135e2008-08-05 17:53:22 +00001238 set y [sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 0]
1239 set val [format {now %10d max %10d max-size %10d} \
1240 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001241 output1 "Page-cache used: $val"
drhf7141992008-06-19 00:16:08 +00001242 set x [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0]
1243 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001244 output1 "Page-cache overflow: $val"
drhf7141992008-06-19 00:16:08 +00001245 set x [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0]
1246 set val [format {now %10d max %10d} [lindex $x 1] [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001247 output1 "Scratch memory used: $val"
drhf7141992008-06-19 00:16:08 +00001248 set x [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0]
drhe50135e2008-08-05 17:53:22 +00001249 set y [sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 0]
1250 set val [format {now %10d max %10d max-size %10d} \
1251 [lindex $x 1] [lindex $x 2] [lindex $y 2]]
dan17c08232015-06-09 15:58:28 +00001252 output1 "Scratch overflow: $val"
drhec424a52008-07-25 15:39:03 +00001253 ifcapable yytrackmaxstackdepth {
1254 set x [sqlite3_status SQLITE_STATUS_PARSER_STACK 0]
1255 set val [format { max %10d} [lindex $x 2]]
dan17c08232015-06-09 15:58:28 +00001256 output2 "Parser stack depth: $val"
drhec424a52008-07-25 15:39:03 +00001257 }
drhf7141992008-06-19 00:16:08 +00001258}
1259
drh348784e2000-05-29 20:41:49 +00001260# A procedure to execute SQL
1261#
drhc4a3c772001-04-04 11:48:57 +00001262proc execsql {sql {db db}} {
drhacbcdc42001-01-22 00:31:53 +00001263 # puts "SQL = $sql"
danielk19773bdca9c2006-01-17 09:35:01 +00001264 uplevel [list $db eval $sql]
drh348784e2000-05-29 20:41:49 +00001265}
drh3c2aeae2014-01-24 14:37:44 +00001266proc execsql_timed {sql {db db}} {
1267 set tm [time {
1268 set x [uplevel [list $db eval $sql]]
1269 } 1]
1270 set tm [lindex $tm 0]
dan17c08232015-06-09 15:58:28 +00001271 output1 -nonewline " ([expr {$tm*0.001}]ms) "
drh3c2aeae2014-01-24 14:37:44 +00001272 set x
1273}
drh3aadb2e2000-05-31 17:59:25 +00001274
drhadbca9c2001-09-27 15:11:53 +00001275# Execute SQL and catch exceptions.
1276#
1277proc catchsql {sql {db db}} {
1278 # puts "SQL = $sql"
dan81fa6dc2009-11-28 12:40:32 +00001279 set r [catch [list uplevel [list $db eval $sql]] msg]
drhadbca9c2001-09-27 15:11:53 +00001280 lappend r $msg
1281 return $r
1282}
1283
drh04096482001-11-09 22:41:44 +00001284# Do an VDBE code dump on the SQL given
1285#
1286proc explain {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001287 output2 ""
1288 output2 "addr opcode p1 p2 p3 p4 p5 #"
1289 output2 "---- ------------ ------ ------ ------ --------------- -- -"
drh04096482001-11-09 22:41:44 +00001290 $db eval "explain $sql" {} {
mistachkineeb31ff2015-06-10 23:02:38 +00001291 output2 [format {%-4d %-12.12s %-6d %-6d %-6d % -17s %s %s} \
danielk1977287fb612008-01-04 19:10:28 +00001292 $addr $opcode $p1 $p2 $p3 $p4 $p5 $comment
1293 ]
drh04096482001-11-09 22:41:44 +00001294 }
1295}
1296
dane83267d2013-11-05 14:19:22 +00001297proc explain_i {sql {db db}} {
mistachkineeb31ff2015-06-10 23:02:38 +00001298 output2 ""
1299 output2 "addr opcode p1 p2 p3 p4 p5 #"
1300 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001301
dane83267d2013-11-05 14:19:22 +00001302
dan5c663c32013-11-06 14:52:40 +00001303 # Set up colors for the different opcodes. Scheme is as follows:
1304 #
1305 # Red: Opcodes that write to a b-tree.
1306 # Blue: Opcodes that reposition or seek a cursor.
1307 # Green: The ResultRow opcode.
1308 #
dandea63f22014-03-03 16:48:47 +00001309 if { [catch {fconfigure stdout -mode}]==0 } {
1310 set R "\033\[31;1m" ;# Red fg
1311 set G "\033\[32;1m" ;# Green fg
1312 set B "\033\[34;1m" ;# Red fg
1313 set D "\033\[39;0m" ;# Default fg
1314 } else {
1315 set R ""
1316 set G ""
1317 set B ""
1318 set D ""
1319 }
dan5c663c32013-11-06 14:52:40 +00001320 foreach opcode {
1321 Seek SeekGe SeekGt SeekLe SeekLt NotFound Last Rewind
dane3ab7292013-11-12 12:30:09 +00001322 NoConflict Next Prev VNext VPrev VFilter
drhf45f2322014-03-23 17:45:03 +00001323 SorterSort SorterNext
dan5c663c32013-11-06 14:52:40 +00001324 } {
1325 set color($opcode) $B
1326 }
1327 foreach opcode {ResultRow} {
1328 set color($opcode) $G
1329 }
1330 foreach opcode {IdxInsert Insert Delete IdxDelete} {
1331 set color($opcode) $R
1332 }
1333
dan427ebba2013-11-05 16:39:31 +00001334 set bSeenGoto 0
dane83267d2013-11-05 14:19:22 +00001335 $db eval "explain $sql" {} {
1336 set x($addr) 0
1337 set op($addr) $opcode
1338
dan427ebba2013-11-05 16:39:31 +00001339 if {$opcode == "Goto" && ($bSeenGoto==0 || ($p2 > $addr+10))} {
1340 set linebreak($p2) 1
1341 set bSeenGoto 1
dane83267d2013-11-05 14:19:22 +00001342 }
1343
dane3ab7292013-11-12 12:30:09 +00001344 if {$opcode=="Next" || $opcode=="Prev"
1345 || $opcode=="VNext" || $opcode=="VPrev"
drhf45f2322014-03-23 17:45:03 +00001346 || $opcode=="SorterNext"
dane3ab7292013-11-12 12:30:09 +00001347 } {
dane83267d2013-11-05 14:19:22 +00001348 for {set i $p2} {$i<$addr} {incr i} {
1349 incr x($i) 2
1350 }
1351 }
1352
1353 if {$opcode == "Goto" && $p2<$addr && $op($p2)=="Yield"} {
1354 for {set i [expr $p2+1]} {$i<$addr} {incr i} {
1355 incr x($i) 2
1356 }
1357 }
dan427ebba2013-11-05 16:39:31 +00001358
1359 if {$opcode == "Halt" && $comment == "End of coroutine"} {
1360 set linebreak([expr $addr+1]) 1
1361 }
dane83267d2013-11-05 14:19:22 +00001362 }
1363
1364 $db eval "explain $sql" {} {
dan427ebba2013-11-05 16:39:31 +00001365 if {[info exists linebreak($addr)]} {
mistachkineeb31ff2015-06-10 23:02:38 +00001366 output2 ""
dane83267d2013-11-05 14:19:22 +00001367 }
1368 set I [string repeat " " $x($addr)]
dan5c663c32013-11-06 14:52:40 +00001369
1370 set col ""
1371 catch { set col $color($opcode) }
1372
mistachkineeb31ff2015-06-10 23:02:38 +00001373 output2 [format {%-4d %s%s%-12.12s%s %-6d %-6d %-6d % -17s %s %s} \
dan5c663c32013-11-06 14:52:40 +00001374 $addr $I $col $opcode $D $p1 $p2 $p3 $p4 $p5 $comment
dane83267d2013-11-05 14:19:22 +00001375 ]
dane83267d2013-11-05 14:19:22 +00001376 }
mistachkineeb31ff2015-06-10 23:02:38 +00001377 output2 "---- ------------ ------ ------ ------ ---------------- -- -"
dane83267d2013-11-05 14:19:22 +00001378}
1379
drhdafc0ce2008-04-17 19:14:02 +00001380# Show the VDBE program for an SQL statement but omit the Trace
1381# opcode at the beginning. This procedure can be used to prove
1382# that different SQL statements generate exactly the same VDBE code.
1383#
1384proc explain_no_trace {sql} {
1385 set tr [db eval "EXPLAIN $sql"]
1386 return [lrange $tr 7 end]
1387}
1388
drh3aadb2e2000-05-31 17:59:25 +00001389# Another procedure to execute SQL. This one includes the field
1390# names in the returned list.
1391#
1392proc execsql2 {sql} {
1393 set result {}
1394 db eval $sql data {
1395 foreach f $data(*) {
1396 lappend result $f $data($f)
1397 }
1398 }
1399 return $result
1400}
drh17a68932001-01-31 13:28:08 +00001401
mistachkin1d406e02013-08-29 01:09:14 +00001402# Use a temporary in-memory database to execute SQL statements
1403#
1404proc memdbsql {sql} {
1405 sqlite3 memdb :memory:
1406 set result [memdb eval $sql]
1407 memdb close
1408 return $result
1409}
1410
drhdde85d92003-03-01 19:45:34 +00001411# Use the non-callback API to execute multiple SQL statements
1412#
1413proc stepsql {dbptr sql} {
1414 set sql [string trim $sql]
1415 set r 0
1416 while {[string length $sql]>0} {
danielk19774ad17132004-05-21 01:47:26 +00001417 if {[catch {sqlite3_prepare $dbptr $sql -1 sqltail} vm]} {
drhdde85d92003-03-01 19:45:34 +00001418 return [list 1 $vm]
1419 }
1420 set sql [string trim $sqltail]
danielk1977fbcd5852004-06-15 02:44:18 +00001421# while {[sqlite_step $vm N VAL COL]=="SQLITE_ROW"} {
1422# foreach v $VAL {lappend r $v}
1423# }
1424 while {[sqlite3_step $vm]=="SQLITE_ROW"} {
1425 for {set i 0} {$i<[sqlite3_data_count $vm]} {incr i} {
1426 lappend r [sqlite3_column_text $vm $i]
1427 }
drhdde85d92003-03-01 19:45:34 +00001428 }
danielk1977106bb232004-05-21 10:08:53 +00001429 if {[catch {sqlite3_finalize $vm} errmsg]} {
drhdde85d92003-03-01 19:45:34 +00001430 return [list 1 $errmsg]
1431 }
1432 }
1433 return $r
1434}
1435
drh21504322002-06-25 13:16:02 +00001436# Do an integrity check of the entire database
1437#
danielk197704103022009-02-03 16:51:24 +00001438proc integrity_check {name {db db}} {
drh27d258a2004-10-30 20:23:09 +00001439 ifcapable integrityck {
danielk197704103022009-02-03 16:51:24 +00001440 do_test $name [list execsql {PRAGMA integrity_check} $db] {ok}
drh27d258a2004-10-30 20:23:09 +00001441 }
1442}
1443
drh433dccf2013-02-09 15:37:11 +00001444# Check the extended error code
1445#
1446proc verify_ex_errcode {name expected {db db}} {
1447 do_test $name [list sqlite3_extended_errcode $db] $expected
1448}
1449
danc6055c72011-04-28 18:46:46 +00001450
1451# Return true if the SQL statement passed as the second argument uses a
1452# statement transaction.
1453#
1454proc sql_uses_stmt {db sql} {
1455 set stmt [sqlite3_prepare $db $sql -1 dummy]
1456 set uses [uses_stmt_journal $stmt]
1457 sqlite3_finalize $stmt
1458 return $uses
1459}
1460
danielk1977db4e8862008-01-16 08:24:46 +00001461proc fix_ifcapable_expr {expr} {
1462 set ret ""
1463 set state 0
1464 for {set i 0} {$i < [string length $expr]} {incr i} {
1465 set char [string range $expr $i $i]
1466 set newstate [expr {[string is alnum $char] || $char eq "_"}]
1467 if {$newstate && !$state} {
1468 append ret {$::sqlite_options(}
1469 }
1470 if {!$newstate && $state} {
1471 append ret )
1472 }
1473 append ret $char
1474 set state $newstate
1475 }
1476 if {$state} {append ret )}
1477 return $ret
1478}
1479
mistachkinc60941f2012-09-13 01:51:02 +00001480# Returns non-zero if the capabilities are present; zero otherwise.
1481#
1482proc capable {expr} {
1483 set e [fix_ifcapable_expr $expr]; return [expr ($e)]
1484}
1485
drh27d258a2004-10-30 20:23:09 +00001486# Evaluate a boolean expression of capabilities. If true, execute the
1487# code. Omit the code if false.
1488#
danielk19773e8c37e2005-01-21 03:12:14 +00001489proc ifcapable {expr code {else ""} {elsecode ""}} {
danielk1977db4e8862008-01-16 08:24:46 +00001490 #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
1491 set e2 [fix_ifcapable_expr $expr]
danielk19773e8c37e2005-01-21 03:12:14 +00001492 if ($e2) {
1493 set c [catch {uplevel 1 $code} r]
1494 } else {
1495 set c [catch {uplevel 1 $elsecode} r]
1496 }
1497 return -code $c $r
drh21504322002-06-25 13:16:02 +00001498}
danielk197745901d62004-11-10 15:27:38 +00001499
danielk1977aca790a2005-01-13 11:07:52 +00001500# This proc execs a seperate process that crashes midway through executing
1501# the SQL script $sql on database test.db.
1502#
1503# The crash occurs during a sync() of file $crashfile. When the crash
1504# occurs a random subset of all unsynced writes made by the process are
1505# written into the files on disk. Argument $crashdelay indicates the
1506# number of file syncs to wait before crashing.
1507#
1508# The return value is a list of two elements. The first element is a
1509# boolean, indicating whether or not the process actually crashed or
1510# reported some other error. The second element in the returned list is the
1511# error message. This is "child process exited abnormally" if the crash
mistachkin48864df2013-03-21 21:20:32 +00001512# occurred.
danielk1977aca790a2005-01-13 11:07:52 +00001513#
danielk1977f8940ae2007-08-23 11:07:10 +00001514# crashsql -delay CRASHDELAY -file CRASHFILE ?-blocksize BLOCKSIZE? $sql
danielk197759a33f92007-03-17 10:26:59 +00001515#
1516proc crashsql {args} {
danielk197759a33f92007-03-17 10:26:59 +00001517
1518 set blocksize ""
1519 set crashdelay 1
drhcb1f0f62008-01-08 15:18:52 +00001520 set prngseed 0
dan999cd082013-12-09 20:42:03 +00001521 set opendb { sqlite3 db test.db -vfs crash }
drhf8587402008-01-08 16:03:49 +00001522 set tclbody {}
danielk197759a33f92007-03-17 10:26:59 +00001523 set crashfile ""
danielk1977f8940ae2007-08-23 11:07:10 +00001524 set dc ""
danielk197759a33f92007-03-17 10:26:59 +00001525 set sql [lindex $args end]
mistachkin1d406e02013-08-29 01:09:14 +00001526
danielk197759a33f92007-03-17 10:26:59 +00001527 for {set ii 0} {$ii < [llength $args]-1} {incr ii 2} {
1528 set z [lindex $args $ii]
1529 set n [string length $z]
1530 set z2 [lindex $args [expr $ii+1]]
1531
1532 if {$n>1 && [string first $z -delay]==0} {set crashdelay $z2} \
dan999cd082013-12-09 20:42:03 +00001533 elseif {$n>1 && [string first $z -opendb]==0} {set opendb $z2} \
drhcb1f0f62008-01-08 15:18:52 +00001534 elseif {$n>1 && [string first $z -seed]==0} {set prngseed $z2} \
danielk197759a33f92007-03-17 10:26:59 +00001535 elseif {$n>1 && [string first $z -file]==0} {set crashfile $z2} \
drhf8587402008-01-08 16:03:49 +00001536 elseif {$n>1 && [string first $z -tclbody]==0} {set tclbody $z2} \
danielk1977967a4a12007-08-20 14:23:44 +00001537 elseif {$n>1 && [string first $z -blocksize]==0} {set blocksize "-s $z2" } \
danielk1977f8940ae2007-08-23 11:07:10 +00001538 elseif {$n>1 && [string first $z -characteristics]==0} {set dc "-c {$z2}" } \
danielk197759a33f92007-03-17 10:26:59 +00001539 else { error "Unrecognized option: $z" }
1540 }
1541
1542 if {$crashfile eq ""} {
1543 error "Compulsory option -file missing"
1544 }
1545
mistachkin1d406e02013-08-29 01:09:14 +00001546 # $crashfile gets compared to the native filename in
shanehf2c08822010-07-08 16:30:44 +00001547 # cfSync(), which can be different then what TCL uses by
1548 # default, so here we force it to the "nativename" format.
mistachkinf8a78462012-03-08 20:00:36 +00001549 set cfile [string map {\\ \\\\} [file nativename [file join [get_pwd] $crashfile]]]
danielk1977aca790a2005-01-13 11:07:52 +00001550
1551 set f [open crash.tcl w]
danielk1977ca0c8972007-09-01 09:02:53 +00001552 puts $f "sqlite3_crash_enable 1"
danielk1977f8940ae2007-08-23 11:07:10 +00001553 puts $f "sqlite3_crashparams $blocksize $dc $crashdelay $cfile"
drhc7a3bb92009-02-05 16:31:45 +00001554 puts $f "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
danielk1977933bbd62007-03-17 07:22:42 +00001555
1556 # This block sets the cache size of the main database to 10
1557 # pages. This is done in case the build is configured to omit
1558 # "PRAGMA cache_size".
danec16d982015-04-17 08:36:05 +00001559 if {$opendb!=""} {
1560 puts $f $opendb
1561 puts $f {db eval {SELECT * FROM sqlite_master;}}
1562 puts $f {set bt [btree_from_db db]}
1563 puts $f {btree_set_cache_size $bt 10}
1564 }
dan999cd082013-12-09 20:42:03 +00001565
drhcb1f0f62008-01-08 15:18:52 +00001566 if {$prngseed} {
1567 set seed [expr {$prngseed%10007+1}]
1568 # puts seed=$seed
1569 puts $f "db eval {SELECT randomblob($seed)}"
1570 }
danielk1977933bbd62007-03-17 07:22:42 +00001571
drhf8587402008-01-08 16:03:49 +00001572 if {[string length $tclbody]>0} {
1573 puts $f $tclbody
1574 }
1575 if {[string length $sql]>0} {
1576 puts $f "db eval {"
1577 puts $f "$sql"
1578 puts $f "}"
1579 }
danielk1977aca790a2005-01-13 11:07:52 +00001580 close $f
danielk1977aca790a2005-01-13 11:07:52 +00001581 set r [catch {
drh9c06c952005-11-26 00:25:00 +00001582 exec [info nameofexec] crash.tcl >@stdout
danielk1977aca790a2005-01-13 11:07:52 +00001583 } msg]
mistachkin1d406e02013-08-29 01:09:14 +00001584
shanehf2c08822010-07-08 16:30:44 +00001585 # Windows/ActiveState TCL returns a slightly different
1586 # error message. We map that to the expected message
1587 # so that we don't have to change all of the test
1588 # cases.
1589 if {$::tcl_platform(platform)=="windows"} {
1590 if {$msg=="child killed: unknown signal"} {
1591 set msg "child process exited abnormally"
1592 }
1593 }
mistachkin1d406e02013-08-29 01:09:14 +00001594
danielk1977aca790a2005-01-13 11:07:52 +00001595 lappend r $msg
1596}
1597
danb88e24f2013-02-23 18:58:11 +00001598proc run_ioerr_prep {} {
1599 set ::sqlite_io_error_pending 0
1600 catch {db close}
1601 catch {db2 close}
1602 catch {forcedelete test.db}
1603 catch {forcedelete test.db-journal}
1604 catch {forcedelete test2.db}
1605 catch {forcedelete test2.db-journal}
1606 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
1607 sqlite3_extended_result_codes $::DB $::ioerropts(-erc)
1608 if {[info exists ::ioerropts(-tclprep)]} {
1609 eval $::ioerropts(-tclprep)
1610 }
1611 if {[info exists ::ioerropts(-sqlprep)]} {
1612 execsql $::ioerropts(-sqlprep)
1613 }
1614 expr 0
1615}
1616
danielk197732554c12005-01-22 03:39:39 +00001617# Usage: do_ioerr_test <test number> <options...>
1618#
1619# This proc is used to implement test cases that check that IO errors
mistachkin1d406e02013-08-29 01:09:14 +00001620# are correctly handled. The first argument, <test number>, is an integer
danielk197732554c12005-01-22 03:39:39 +00001621# used to name the tests executed by this proc. Options are as follows:
1622#
1623# -tclprep TCL script to run to prepare test.
1624# -sqlprep SQL script to run to prepare test.
1625# -tclbody TCL script to run with IO error simulation.
1626# -sqlbody TCL script to run with IO error simulation.
1627# -exclude List of 'N' values not to test.
drh4ac285a2006-09-15 07:28:50 +00001628# -erc Use extended result codes
drhd5eb79e2007-03-15 12:17:42 +00001629# -persist Make simulated I/O errors persistent
danielk197732554c12005-01-22 03:39:39 +00001630# -start Value of 'N' to begin with (default 1)
1631#
1632# -cksum Boolean. If true, test that the database does
1633# not change during the execution of the test case.
1634#
1635proc do_ioerr_test {testname args} {
1636
danielk197732554c12005-01-22 03:39:39 +00001637 set ::ioerropts(-start) 1
1638 set ::ioerropts(-cksum) 0
drh4ac285a2006-09-15 07:28:50 +00001639 set ::ioerropts(-erc) 0
drhc2ee76c2007-01-04 14:58:14 +00001640 set ::ioerropts(-count) 100000000
drhd5eb79e2007-03-15 12:17:42 +00001641 set ::ioerropts(-persist) 1
danielk19774abd5442008-05-05 15:26:50 +00001642 set ::ioerropts(-ckrefcount) 0
danielk1977861f7452008-06-05 11:39:11 +00001643 set ::ioerropts(-restoreprng) 1
danielk197732554c12005-01-22 03:39:39 +00001644 array set ::ioerropts $args
1645
danielk197747cd39c2008-05-12 12:41:15 +00001646 # TEMPORARY: For 3.5.9, disable testing of extended result codes. There are
1647 # a couple of obscure IO errors that do not return them.
1648 set ::ioerropts(-erc) 0
1649
danb88e24f2013-02-23 18:58:11 +00001650 # Create a single TCL script from the TCL and SQL specified
1651 # as the body of the test.
1652 set ::ioerrorbody {}
1653 if {[info exists ::ioerropts(-tclbody)]} {
1654 append ::ioerrorbody "$::ioerropts(-tclbody)\n"
1655 }
1656 if {[info exists ::ioerropts(-sqlbody)]} {
1657 append ::ioerrorbody "db eval {$::ioerropts(-sqlbody)}"
1658 }
1659
1660 save_prng_state
1661 if {$::ioerropts(-cksum)} {
1662 run_ioerr_prep
1663 eval $::ioerrorbody
1664 set ::goodcksum [cksum]
1665 }
danielk197732554c12005-01-22 03:39:39 +00001666
danielk197792d4d7a2007-05-04 12:05:56 +00001667 set ::go 1
drhc2ee76c2007-01-04 14:58:14 +00001668 #reset_prng_state
danielk1977ef165ce2009-04-06 17:50:03 +00001669 for {set n $::ioerropts(-start)} {$::go} {incr n} {
drhc2ee76c2007-01-04 14:58:14 +00001670 set ::TN $n
1671 incr ::ioerropts(-count) -1
danielk197732554c12005-01-22 03:39:39 +00001672 if {$::ioerropts(-count)<0} break
mistachkin1d406e02013-08-29 01:09:14 +00001673
danielk197732554c12005-01-22 03:39:39 +00001674 # Skip this IO error if it was specified with the "-exclude" option.
1675 if {[info exists ::ioerropts(-exclude)]} {
1676 if {[lsearch $::ioerropts(-exclude) $n]!=-1} continue
1677 }
danielk1977861f7452008-06-05 11:39:11 +00001678 if {$::ioerropts(-restoreprng)} {
1679 restore_prng_state
1680 }
danielk197732554c12005-01-22 03:39:39 +00001681
mistachkin1d406e02013-08-29 01:09:14 +00001682 # Delete the files test.db and test2.db, then execute the TCL and
danielk197732554c12005-01-22 03:39:39 +00001683 # SQL (in that order) to prepare for the test case.
1684 do_test $testname.$n.1 {
danb88e24f2013-02-23 18:58:11 +00001685 run_ioerr_prep
danielk197732554c12005-01-22 03:39:39 +00001686 } {0}
1687
1688 # Read the 'checksum' of the database.
1689 if {$::ioerropts(-cksum)} {
danb88e24f2013-02-23 18:58:11 +00001690 set ::checksum [cksum]
danielk197732554c12005-01-22 03:39:39 +00001691 }
drh93aed5a2008-01-16 17:46:38 +00001692
danielk197732554c12005-01-22 03:39:39 +00001693 # Set the Nth IO error to fail.
1694 do_test $testname.$n.2 [subst {
drhd5eb79e2007-03-15 12:17:42 +00001695 set ::sqlite_io_error_persist $::ioerropts(-persist)
danielk197732554c12005-01-22 03:39:39 +00001696 set ::sqlite_io_error_pending $n
1697 }] $n
danielk197732554c12005-01-22 03:39:39 +00001698
danb88e24f2013-02-23 18:58:11 +00001699 # Execute the TCL script created for the body of this test. If
mistachkin1d406e02013-08-29 01:09:14 +00001700 # at least N IO operations performed by SQLite as a result of
danb88e24f2013-02-23 18:58:11 +00001701 # the script, the Nth will fail.
danielk197732554c12005-01-22 03:39:39 +00001702 do_test $testname.$n.3 {
drh1aa5af12008-03-07 19:51:14 +00001703 set ::sqlite_io_error_hit 0
1704 set ::sqlite_io_error_hardhit 0
danielk197732554c12005-01-22 03:39:39 +00001705 set r [catch $::ioerrorbody msg]
drh1aa5af12008-03-07 19:51:14 +00001706 set ::errseen $r
drhe49f9822006-09-15 12:29:16 +00001707 set rc [sqlite3_errcode $::DB]
1708 if {$::ioerropts(-erc)} {
drh5f7b5bf2007-04-19 12:30:54 +00001709 # If we are in extended result code mode, make sure all of the
1710 # IOERRs we get back really do have their extended code values.
1711 # If an extended result code is returned, the sqlite3_errcode
1712 # TCLcommand will return a string of the form: SQLITE_IOERR+nnnn
1713 # where nnnn is a number
drhe49f9822006-09-15 12:29:16 +00001714 if {[regexp {^SQLITE_IOERR} $rc] && ![regexp {IOERR\+\d} $rc]} {
1715 return $rc
1716 }
1717 } else {
drh5f7b5bf2007-04-19 12:30:54 +00001718 # If we are not in extended result code mode, make sure no
1719 # extended error codes are returned.
drhe49f9822006-09-15 12:29:16 +00001720 if {[regexp {\+\d} $rc]} {
1721 return $rc
1722 }
1723 }
drh93aed5a2008-01-16 17:46:38 +00001724 # The test repeats as long as $::go is non-zero. $::go starts out
1725 # as 1. When a test runs to completion without hitting an I/O
1726 # error, that means there is no point in continuing with this test
1727 # case so set $::go to zero.
1728 #
1729 if {$::sqlite_io_error_pending>0} {
1730 set ::go 0
1731 set q 0
1732 set ::sqlite_io_error_pending 0
1733 } else {
1734 set q 1
1735 }
1736
drhc9ac5ca2005-11-04 22:03:30 +00001737 set s [expr $::sqlite_io_error_hit==0]
drh1aa5af12008-03-07 19:51:14 +00001738 if {$::sqlite_io_error_hit>$::sqlite_io_error_hardhit && $r==0} {
1739 set r 1
1740 }
danielk1977f2fa8312006-01-24 13:09:33 +00001741 set ::sqlite_io_error_hit 0
drh5f7b5bf2007-04-19 12:30:54 +00001742
1743 # One of two things must have happened. either
1744 # 1. We never hit the IO error and the SQL returned OK
1745 # 2. An IO error was hit and the SQL failed
1746 #
dand41a29a2010-05-06 15:56:28 +00001747 #puts "s=$s r=$r q=$q"
drh93aed5a2008-01-16 17:46:38 +00001748 expr { ($s && !$r && !$q) || (!$s && $r && $q) }
danielk197732554c12005-01-22 03:39:39 +00001749 } {1}
1750
danielk197780daec62008-05-12 10:57:02 +00001751 set ::sqlite_io_error_hit 0
1752 set ::sqlite_io_error_pending 0
1753
mistachkin1d406e02013-08-29 01:09:14 +00001754 # Check that no page references were leaked. There should be
1755 # a single reference if there is still an active transaction,
danielk197752b472a2008-05-05 16:23:55 +00001756 # or zero otherwise.
1757 #
danielk197781620542008-06-07 05:19:37 +00001758 # UPDATE: If the IO error occurs after a 'BEGIN' but before any
mistachkin1d406e02013-08-29 01:09:14 +00001759 # locks are established on database files (i.e. if the error
danielk197781620542008-06-07 05:19:37 +00001760 # occurs while attempting to detect a hot-journal file), then
1761 # there may 0 page references and an active transaction according
1762 # to [sqlite3_get_autocommit].
1763 #
danielk19774abd5442008-05-05 15:26:50 +00001764 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-ckrefcount)} {
danielk19774abd5442008-05-05 15:26:50 +00001765 do_test $testname.$n.4 {
1766 set bt [btree_from_db db]
1767 db_enter db
1768 array set stats [btree_pager_stats $bt]
1769 db_leave db
danielk197781620542008-06-07 05:19:37 +00001770 set nRef $stats(ref)
1771 expr {$nRef == 0 || ([sqlite3_get_autocommit db]==0 && $nRef == 1)}
1772 } {1}
danielk19774abd5442008-05-05 15:26:50 +00001773 }
1774
mistachkin1d406e02013-08-29 01:09:14 +00001775 # If there is an open database handle and no open transaction,
danielk197752b472a2008-05-05 16:23:55 +00001776 # and the pager is not running in exclusive-locking mode,
1777 # check that the pager is in "unlocked" state. Theoretically,
1778 # if a call to xUnlock() failed due to an IO error the underlying
1779 # file may still be locked.
1780 #
1781 ifcapable pragma {
1782 if { [info commands db] ne ""
danielk19770259fbe2008-05-05 17:14:53 +00001783 && $::ioerropts(-ckrefcount)
danielk197752b472a2008-05-05 16:23:55 +00001784 && [db one {pragma locking_mode}] eq "normal"
1785 && [sqlite3_get_autocommit db]
1786 } {
1787 do_test $testname.$n.5 {
1788 set bt [btree_from_db db]
1789 db_enter db
1790 array set stats [btree_pager_stats $bt]
1791 db_leave db
1792 set stats(state)
1793 } 0
1794 }
1795 }
1796
mistachkin48864df2013-03-21 21:20:32 +00001797 # If an IO error occurred, then the checksum of the database should
danielk197732554c12005-01-22 03:39:39 +00001798 # be the same as before the script that caused the IO error was run.
danielk197752b472a2008-05-05 16:23:55 +00001799 #
drh1aa5af12008-03-07 19:51:14 +00001800 if {$::go && $::sqlite_io_error_hardhit && $::ioerropts(-cksum)} {
danielk19770259fbe2008-05-05 17:14:53 +00001801 do_test $testname.$n.6 {
danielk197732554c12005-01-22 03:39:39 +00001802 catch {db close}
danielk1977a9613392008-07-08 12:07:32 +00001803 catch {db2 close}
drha34c62d2006-01-06 22:11:20 +00001804 set ::DB [sqlite3 db test.db; sqlite3_connection_pointer db]
danb88e24f2013-02-23 18:58:11 +00001805 set nowcksum [cksum]
1806 set res [expr {$nowcksum==$::checksum || $nowcksum==$::goodcksum}]
1807 if {$res==0} {
dan17c08232015-06-09 15:58:28 +00001808 output2 "now=$nowcksum"
1809 output2 "the=$::checksum"
1810 output2 "fwd=$::goodcksum"
danb88e24f2013-02-23 18:58:11 +00001811 }
1812 set res
1813 } 1
danielk197732554c12005-01-22 03:39:39 +00001814 }
1815
drh1aa5af12008-03-07 19:51:14 +00001816 set ::sqlite_io_error_hardhit 0
danielk1977c4da5b92006-01-21 12:08:54 +00001817 set ::sqlite_io_error_pending 0
danielk1977105afed2005-05-26 15:20:53 +00001818 if {[info exists ::ioerropts(-cleanup)]} {
1819 catch $::ioerropts(-cleanup)
1820 }
danielk197732554c12005-01-22 03:39:39 +00001821 }
1822 set ::sqlite_io_error_pending 0
drh6d54da02007-03-25 19:08:46 +00001823 set ::sqlite_io_error_persist 0
danielk197732554c12005-01-22 03:39:39 +00001824 unset ::ioerropts
1825}
1826
drh93aed5a2008-01-16 17:46:38 +00001827# Return a checksum based on the contents of the main database associated
1828# with connection $db
danielk197732554c12005-01-22 03:39:39 +00001829#
1830proc cksum {{db db}} {
1831 set txt [$db eval {
1832 SELECT name, type, sql FROM sqlite_master order by name
1833 }]\n
1834 foreach tbl [$db eval {
1835 SELECT name FROM sqlite_master WHERE type='table' order by name
1836 }] {
1837 append txt [$db eval "SELECT * FROM $tbl"]\n
1838 }
1839 foreach prag {default_synchronous default_cache_size} {
1840 append txt $prag-[$db eval "PRAGMA $prag"]\n
1841 }
1842 set cksum [string length $txt]-[md5 $txt]
1843 # puts $cksum-[file size test.db]
1844 return $cksum
1845}
1846
drh93aed5a2008-01-16 17:46:38 +00001847# Generate a checksum based on the contents of the main and temp tables
1848# database $db. If the checksum of two databases is the same, and the
1849# integrity-check passes for both, the two databases are identical.
1850#
drh1db639c2008-01-17 02:36:28 +00001851proc allcksum {{db db}} {
drh93aed5a2008-01-16 17:46:38 +00001852 set ret [list]
1853 ifcapable tempdb {
1854 set sql {
1855 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1856 SELECT name FROM sqlite_temp_master WHERE type = 'table' UNION
1857 SELECT 'sqlite_master' UNION
1858 SELECT 'sqlite_temp_master' ORDER BY 1
1859 }
1860 } else {
1861 set sql {
1862 SELECT name FROM sqlite_master WHERE type = 'table' UNION
1863 SELECT 'sqlite_master' ORDER BY 1
1864 }
1865 }
1866 set tbllist [$db eval $sql]
1867 set txt {}
1868 foreach tbl $tbllist {
1869 append txt [$db eval "SELECT * FROM $tbl"]
1870 }
1871 foreach prag {default_cache_size} {
1872 append txt $prag-[$db eval "PRAGMA $prag"]\n
1873 }
1874 # puts txt=$txt
1875 return [md5 $txt]
1876}
1877
drhdc2c4912009-02-04 22:46:47 +00001878# Generate a checksum based on the contents of a single database with
mistachkin1d406e02013-08-29 01:09:14 +00001879# a database connection. The name of the database is $dbname.
drhdc2c4912009-02-04 22:46:47 +00001880# Examples of $dbname are "temp" or "main".
1881#
1882proc dbcksum {db dbname} {
1883 if {$dbname=="temp"} {
1884 set master sqlite_temp_master
1885 } else {
1886 set master $dbname.sqlite_master
1887 }
1888 set alltab [$db eval "SELECT name FROM $master WHERE type='table'"]
1889 set txt [$db eval "SELECT * FROM $master"]\n
1890 foreach tab $alltab {
1891 append txt [$db eval "SELECT * FROM $dbname.$tab"]\n
1892 }
1893 return [md5 $txt]
1894}
1895
danielk197735754ac2008-03-21 17:29:37 +00001896proc memdebug_log_sql {{filename mallocs.sql}} {
1897
danielk19776f332c12008-03-21 14:22:44 +00001898 set data [sqlite3_memdebug_log dump]
1899 set nFrame [expr [llength [lindex $data 0]]-2]
danielk19776f332c12008-03-21 14:22:44 +00001900 if {$nFrame < 0} { return "" }
1901
danielk197735754ac2008-03-21 17:29:37 +00001902 set database temp
1903
danielk19776ab3a2e2009-02-19 14:39:25 +00001904 set tbl "CREATE TABLE ${database}.malloc(zTest, nCall, nByte, lStack);"
danielk19776f332c12008-03-21 14:22:44 +00001905
1906 set sql ""
1907 foreach e $data {
danielk19776ab3a2e2009-02-19 14:39:25 +00001908 set nCall [lindex $e 0]
1909 set nByte [lindex $e 1]
1910 set lStack [lrange $e 2 end]
1911 append sql "INSERT INTO ${database}.malloc VALUES"
1912 append sql "('test', $nCall, $nByte, '$lStack');\n"
1913 foreach f $lStack {
danielk19776f332c12008-03-21 14:22:44 +00001914 set frames($f) 1
1915 }
1916 }
1917
1918 set tbl2 "CREATE TABLE ${database}.frame(frame INTEGER PRIMARY KEY, line);\n"
danielk197735754ac2008-03-21 17:29:37 +00001919 set tbl3 "CREATE TABLE ${database}.file(name PRIMARY KEY, content);\n"
danielk19776f332c12008-03-21 14:22:44 +00001920
1921 foreach f [array names frames] {
1922 set addr [format %x $f]
1923 set cmd "addr2line -e [info nameofexec] $addr"
1924 set line [eval exec $cmd]
1925 append sql "INSERT INTO ${database}.frame VALUES($f, '$line');\n"
danielk197735754ac2008-03-21 17:29:37 +00001926
1927 set file [lindex [split $line :] 0]
1928 set files($file) 1
danielk19776f332c12008-03-21 14:22:44 +00001929 }
1930
danielk197735754ac2008-03-21 17:29:37 +00001931 foreach f [array names files] {
1932 set contents ""
1933 catch {
1934 set fd [open $f]
1935 set contents [read $fd]
1936 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001937 }
danielk197735754ac2008-03-21 17:29:37 +00001938 set contents [string map {' ''} $contents]
1939 append sql "INSERT INTO ${database}.file VALUES('$f', '$contents');\n"
danielk19776f332c12008-03-21 14:22:44 +00001940 }
danielk19776f332c12008-03-21 14:22:44 +00001941
danielk197735754ac2008-03-21 17:29:37 +00001942 set fd [open $filename w]
1943 puts $fd "BEGIN; ${tbl}${tbl2}${tbl3}${sql} ; COMMIT;"
1944 close $fd
danielk19776f332c12008-03-21 14:22:44 +00001945}
drh93aed5a2008-01-16 17:46:38 +00001946
danf5894502009-10-07 18:41:19 +00001947# Drop all tables in database [db]
1948proc drop_all_tables {{db db}} {
shaneh4e7b32f2009-12-17 22:12:51 +00001949 ifcapable trigger&&foreignkey {
1950 set pk [$db one "PRAGMA foreign_keys"]
1951 $db eval "PRAGMA foreign_keys = OFF"
1952 }
drh9a6ffc82010-02-15 18:03:20 +00001953 foreach {idx name file} [db eval {PRAGMA database_list}] {
1954 if {$idx==1} {
1955 set master sqlite_temp_master
1956 } else {
1957 set master $name.sqlite_master
1958 }
1959 foreach {t type} [$db eval "
1960 SELECT name, type FROM $master
dana16d1062010-09-28 17:37:28 +00001961 WHERE type IN('table', 'view') AND name NOT LIKE 'sqliteX_%' ESCAPE 'X'
drh9a6ffc82010-02-15 18:03:20 +00001962 "] {
dana16d1062010-09-28 17:37:28 +00001963 $db eval "DROP $type \"$t\""
drh9a6ffc82010-02-15 18:03:20 +00001964 }
danf5894502009-10-07 18:41:19 +00001965 }
shaneh4e7b32f2009-12-17 22:12:51 +00001966 ifcapable trigger&&foreignkey {
1967 $db eval "PRAGMA foreign_keys = $pk"
1968 }
danf5894502009-10-07 18:41:19 +00001969}
1970
dan71cb5182010-04-26 12:39:03 +00001971#-------------------------------------------------------------------------
dan430e74c2010-06-07 17:47:26 +00001972# If a test script is executed with global variable $::G(perm:name) set to
mistachkin1d406e02013-08-29 01:09:14 +00001973# "wal", then the tests are run in WAL mode. Otherwise, they should be run
1974# in rollback mode. The following Tcl procs are used to make this less
dan430e74c2010-06-07 17:47:26 +00001975# intrusive:
dan71cb5182010-04-26 12:39:03 +00001976#
1977# wal_set_journal_mode ?DB?
1978#
1979# If running a WAL test, execute "PRAGMA journal_mode = wal" using
1980# connection handle DB. Otherwise, this command is a no-op.
1981#
1982# wal_check_journal_mode TESTNAME ?DB?
1983#
1984# If running a WAL test, execute a tests case that fails if the main
1985# database for connection handle DB is not currently a WAL database.
1986# Otherwise (if not running a WAL permutation) this is a no-op.
1987#
1988# wal_is_wal_mode
mistachkin1d406e02013-08-29 01:09:14 +00001989#
dan71cb5182010-04-26 12:39:03 +00001990# Returns true if this test should be run in WAL mode. False otherwise.
mistachkin1d406e02013-08-29 01:09:14 +00001991#
dan71cb5182010-04-26 12:39:03 +00001992proc wal_is_wal_mode {} {
dan430e74c2010-06-07 17:47:26 +00001993 expr {[permutation] eq "wal"}
dan71cb5182010-04-26 12:39:03 +00001994}
1995proc wal_set_journal_mode {{db db}} {
1996 if { [wal_is_wal_mode] } {
1997 $db eval "PRAGMA journal_mode = WAL"
1998 }
1999}
2000proc wal_check_journal_mode {testname {db db}} {
2001 if { [wal_is_wal_mode] } {
2002 $db eval { SELECT * FROM sqlite_master }
2003 do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal}
2004 }
2005}
2006
dan05accd22016-04-27 18:54:49 +00002007proc wal_is_capable {} {
2008 ifcapable !wal { return 0 }
2009 if {[permutation]=="journaltest"} { return 0 }
2010 return 1
2011}
2012
dan430e74c2010-06-07 17:47:26 +00002013proc permutation {} {
2014 set perm ""
2015 catch {set perm $::G(perm:name)}
2016 set perm
2017}
dancb79e512010-08-06 13:50:07 +00002018proc presql {} {
2019 set presql ""
2020 catch {set presql $::G(perm:presql)}
2021 set presql
2022}
dan430e74c2010-06-07 17:47:26 +00002023
adam45d55a22010-08-18 00:09:47 +00002024proc wal_is_ok {} {
2025 if { [forced_proxy_locking] } {
adam985be1b2010-11-09 00:43:59 +00002026 return 1
adam45d55a22010-08-18 00:09:47 +00002027 }
2028 if { ![path_is_local "."] } {
2029 return 0
2030 }
2031 if { [path_is_dos "."] } {
2032 return 0
2033 }
2034 return 1
2035}
2036
danbe7721d2016-02-04 17:31:03 +00002037proc isquick {} {
2038 set ret 0
2039 catch {set ret $::G(isquick)}
2040 set ret
2041}
2042
danc1a60c52010-06-07 14:28:16 +00002043#-------------------------------------------------------------------------
2044#
2045proc slave_test_script {script} {
2046
2047 # Create the interpreter used to run the test script.
2048 interp create tinterp
2049
2050 # Populate some global variables that tester.tcl expects to see.
2051 foreach {var value} [list \
2052 ::argv0 $::argv0 \
2053 ::argv {} \
2054 ::SLAVE 1 \
2055 ] {
2056 interp eval tinterp [list set $var $value]
2057 }
2058
dan17c08232015-06-09 15:58:28 +00002059 # If output is being copied into a file, share the file-descriptor with
2060 # the interpreter.
2061 if {[info exists ::G(output_fd)]} {
2062 interp share {} $::G(output_fd) tinterp
2063 }
2064
danc1a60c52010-06-07 14:28:16 +00002065 # The alias used to access the global test counters.
2066 tinterp alias set_test_counter set_test_counter
2067
2068 # Set up the ::cmdlinearg array in the slave.
2069 interp eval tinterp [list array set ::cmdlinearg [array get ::cmdlinearg]]
2070
dan430e74c2010-06-07 17:47:26 +00002071 # Set up the ::G array in the slave.
2072 interp eval tinterp [list array set ::G [array get ::G]]
2073
danc1a60c52010-06-07 14:28:16 +00002074 # Load the various test interfaces implemented in C.
2075 load_testfixture_extensions tinterp
2076
2077 # Run the test script.
2078 interp eval tinterp $script
2079
danea5542d2010-07-06 11:26:15 +00002080 # Check if the interpreter call [run_thread_tests]
2081 if { [interp eval tinterp {info exists ::run_thread_tests_called}] } {
2082 set ::run_thread_tests_called 1
2083 }
2084
danc1a60c52010-06-07 14:28:16 +00002085 # Delete the interpreter used to run the test script.
2086 interp delete tinterp
2087}
2088
dan430e74c2010-06-07 17:47:26 +00002089proc slave_test_file {zFile} {
dan0626dfc2010-06-15 06:56:37 +00002090 set tail [file tail $zFile]
dan430e74c2010-06-07 17:47:26 +00002091
dan6a64d672011-04-04 15:38:16 +00002092 if {[info exists ::G(start:permutation)]} {
2093 if {[permutation] != $::G(start:permutation)} return
2094 unset ::G(start:permutation)
2095 }
2096 if {[info exists ::G(start:file)]} {
2097 if {$tail != $::G(start:file) && $tail!="$::G(start:file).test"} return
2098 unset ::G(start:file)
2099 }
2100
dan92d516a2010-07-05 14:54:48 +00002101 # Remember the value of the shared-cache setting. So that it is possible
2102 # to check afterwards that it was not modified by the test script.
2103 #
2104 ifcapable shared_cache { set scs [sqlite3_enable_shared_cache] }
dan0626dfc2010-06-15 06:56:37 +00002105
dan92d516a2010-07-05 14:54:48 +00002106 # Run the test script in a slave interpreter.
2107 #
danea5542d2010-07-06 11:26:15 +00002108 unset -nocomplain ::run_thread_tests_called
dan0626dfc2010-06-15 06:56:37 +00002109 reset_prng_state
2110 set ::sqlite_open_file_count 0
2111 set time [time { slave_test_script [list source $zFile] }]
2112 set ms [expr [lindex $time 0] / 1000]
2113
dan92d516a2010-07-05 14:54:48 +00002114 # Test that all files opened by the test script were closed. Omit this
2115 # if the test script has "thread" in its name. The open file counter
2116 # is not thread-safe.
dan0626dfc2010-06-15 06:56:37 +00002117 #
danea5542d2010-07-06 11:26:15 +00002118 if {[info exists ::run_thread_tests_called]==0} {
dan92d516a2010-07-05 14:54:48 +00002119 do_test ${tail}-closeallfiles { expr {$::sqlite_open_file_count>0} } {0}
2120 }
dan430e74c2010-06-07 17:47:26 +00002121 set ::sqlite_open_file_count 0
2122
mistachkin1d406e02013-08-29 01:09:14 +00002123 # Test that the global "shared-cache" setting was not altered by
dan0626dfc2010-06-15 06:56:37 +00002124 # the test script.
2125 #
mistachkin1d406e02013-08-29 01:09:14 +00002126 ifcapable shared_cache {
dan0626dfc2010-06-15 06:56:37 +00002127 set res [expr {[sqlite3_enable_shared_cache] == $scs}]
2128 do_test ${tail}-sharedcachesetting [list set {} $res] 1
2129 }
dan430e74c2010-06-07 17:47:26 +00002130
dan92d516a2010-07-05 14:54:48 +00002131 # Add some info to the output.
2132 #
dan17c08232015-06-09 15:58:28 +00002133 output2 "Time: $tail $ms ms"
dan430e74c2010-06-07 17:47:26 +00002134 show_memstats
danc1a60c52010-06-07 14:28:16 +00002135}
2136
drhc55e9b82010-08-04 11:59:06 +00002137# Open a new connection on database test.db and execute the SQL script
2138# supplied as an argument. Before returning, close the new conection and
2139# restore the 4 byte fields starting at header offsets 28, 92 and 96
2140# to the values they held before the SQL was executed. This simulates
2141# a write by a pre-3.7.0 client.
2142#
2143proc sql36231 {sql} {
2144 set B [hexio_read test.db 92 8]
2145 set A [hexio_read test.db 28 4]
2146 sqlite3 db36231 test.db
2147 catch { db36231 func a_string a_string }
2148 execsql $sql db36231
2149 db36231 close
2150 hexio_write test.db 28 $A
2151 hexio_write test.db 92 $B
2152 return ""
2153}
danf5894502009-10-07 18:41:19 +00002154
danccc7ff42010-11-29 12:06:45 +00002155proc db_save {} {
2156 foreach f [glob -nocomplain sv_test.db*] { forcedelete $f }
2157 foreach f [glob -nocomplain test.db*] {
2158 set f2 "sv_$f"
mistachkinfda06be2011-08-02 00:57:34 +00002159 forcecopy $f $f2
danccc7ff42010-11-29 12:06:45 +00002160 }
2161}
2162proc db_save_and_close {} {
2163 db_save
2164 catch { db close }
2165 return ""
2166}
2167proc db_restore {} {
2168 foreach f [glob -nocomplain test.db*] { forcedelete $f }
2169 foreach f2 [glob -nocomplain sv_test.db*] {
2170 set f [string range $f2 3 end]
mistachkinfda06be2011-08-02 00:57:34 +00002171 forcecopy $f2 $f
danccc7ff42010-11-29 12:06:45 +00002172 }
2173}
2174proc db_restore_and_reopen {{dbfile test.db}} {
2175 catch { db close }
2176 db_restore
2177 sqlite3 db $dbfile
2178}
2179proc db_delete_and_reopen {{file test.db}} {
2180 catch { db close }
mistachkinfda06be2011-08-02 00:57:34 +00002181 foreach f [glob -nocomplain test.db*] { forcedelete $f }
danccc7ff42010-11-29 12:06:45 +00002182 sqlite3 db $file
2183}
2184
dan03bc5252015-07-24 14:17:17 +00002185# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2186# to configure the size of the PAGECACHE allocation using the parameters
2187# provided to this command. Save the old PAGECACHE parameters in a global
2188# variable so that [test_restore_config_pagecache] can restore the previous
2189# configuration.
2190#
2191# Before returning, reopen connection [db] on file test.db.
2192#
2193proc test_set_config_pagecache {sz nPg} {
2194 catch {db close}
2195 catch {db2 close}
2196 catch {db3 close}
2197
2198 sqlite3_shutdown
2199 set ::old_pagecache_config [sqlite3_config_pagecache $sz $nPg]
2200 sqlite3_initialize
2201 autoinstall_test_functions
dand716c392015-07-24 18:22:29 +00002202 reset_db
dan03bc5252015-07-24 14:17:17 +00002203}
2204
2205# Close any connections named [db], [db2] or [db3]. Then use sqlite3_config
2206# to configure the size of the PAGECACHE allocation to the size saved in
2207# the global variable by an earlier call to [test_set_config_pagecache].
2208#
2209# Before returning, reopen connection [db] on file test.db.
2210#
2211proc test_restore_config_pagecache {} {
2212 catch {db close}
2213 catch {db2 close}
2214 catch {db3 close}
2215
2216 sqlite3_shutdown
2217 eval sqlite3_config_pagecache $::old_pagecache_config
2218 unset ::old_pagecache_config
2219 sqlite3_initialize
2220 autoinstall_test_functions
2221 sqlite3 db test.db
2222}
2223
dan1e8dae02016-03-19 14:53:36 +00002224proc test_find_binary {nm} {
dan089555c2016-03-15 09:55:44 +00002225 if {$::tcl_platform(platform)=="windows"} {
dan1e8dae02016-03-19 14:53:36 +00002226 set ret "$nm.exe"
dan089555c2016-03-15 09:55:44 +00002227 } else {
dan1e8dae02016-03-19 14:53:36 +00002228 set ret $nm
dan089555c2016-03-15 09:55:44 +00002229 }
2230 set ret [file normalize [file join $::cmdlinearg(TESTFIXTURE_HOME) $ret]]
2231 if {![file executable $ret]} {
2232 finish_test
dan49aed582016-03-19 15:13:59 +00002233 return ""
dan089555c2016-03-15 09:55:44 +00002234 }
2235 return $ret
2236}
2237
danielk197745901d62004-11-10 15:27:38 +00002238# Find the name of the 'shell' executable (e.g. "sqlite3.exe") to use for
2239# the tests in shell[1-5].test. If no such executable can be found, invoke
2240# [finish_test ; return] in the callers context.
danielk1977ee8b7992009-03-26 17:13:06 +00002241#
dan64b95bb2012-05-12 05:30:29 +00002242proc test_find_cli {} {
danf27d7372016-03-19 17:48:12 +00002243 set prog [test_find_binary sqlite3]
dan49aed582016-03-19 15:13:59 +00002244 if {$prog==""} { return -code return }
2245 return $prog
drh348784e2000-05-29 20:41:49 +00002246}
2247
dan1e8dae02016-03-19 14:53:36 +00002248# Find the name of the 'sqldiff' executable (e.g. "sqlite3.exe") to use for
2249# the tests in sqldiff tests. If no such executable can be found, invoke
2250# [finish_test ; return] in the callers context.
2251#
2252proc test_find_sqldiff {} {
dan49aed582016-03-19 15:13:59 +00002253 set prog [test_find_binary sqldiff]
2254 if {$prog==""} { return -code return }
2255 return $prog
dan1e8dae02016-03-19 14:53:36 +00002256}
2257
2258
drh348784e2000-05-29 20:41:49 +00002259# If the library is compiled with the SQLITE_DEFAULT_AUTOVACUUM macro set
2260# to non-zero, then set the global variable $AUTOVACUUM to 1.
2261set AUTOVACUUM $sqlite_options(default_autovacuum)
2262
dan64b95bb2012-05-12 05:30:29 +00002263# Make sure the FTS enhanced query syntax is disabled.
2264set sqlite_fts3_enable_parentheses 0
2265
drh348784e2000-05-29 20:41:49 +00002266# During testing, assume that all database files are well-formed. The
2267# few test cases that deliberately corrupt database files should rescind
2268# this setting by invoking "database_can_be_corrupt"
2269#
2270database_never_corrupt
2271
drh348784e2000-05-29 20:41:49 +00002272source $testdir/thread_common.tcl
2273source $testdir/malloc_common.tcl